当用户使用Mapkit在Google地图上放大或缩小时,我们需要获得当前选定的MKMapView缩放级别.
我们尝试过的解决方案是在模拟器上运行良好但在真实设备上运行(iPhone 3GS有iOS 3.0.1).我们需要在iOS 3+(包括iOS 4)上运行它
以下是我们用于获取缩放级别的代码: -
在放大/缩小时,Mapkit会调用此委托: -
(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
Run Code Online (Sandbox Code Playgroud)
它反过来调用我们的Mapzoomlevel函数: -
#define MERCATOR_RADIUS 85445659.44705395
-(float) Mapzoomlevel {
return 21 - round(log2(_mapView.region.span.longitudeDelta *
MERCATOR_RADIUS * M_PI / (180.0 * _mapView.bounds.size.width)));
}
Run Code Online (Sandbox Code Playgroud)
此代码取自(请参阅此博客上发布的缩放级别示例第一条评论): -
http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/
嗨我有一个使用地图和位置的iPhone应用程序.
我希望用户能够按下一个按钮,该按钮可以转向该位置.我知道我不允许在应用程序中执行此操作,但我想知道是否有人可以告诉我是否可以链接到本地谷歌地图应用程序,该应用程序将从用户当前位置输入到该位置的路线.
如果有可能你还可以告诉我怎么样?
任何帮助,将不胜感激.
谢谢
我有一个自定义UIView,当用户点击一个自定义注释时,我将其显示为标注MKMapView.
为了达到这个目的,我已经按照本答案中的建议MKAnnotationView对子-setSelected:selected animated:方法进行了子类化和重载.基本上,我将我的自定义视图添加为我的子类的子视图.MKAnnotationView
问题是我无法与标注进行交互,标注包含一个按钮和一个可滚动的webview.更重要的是,如果标注隐藏了注释并且我在该隐藏注释的大致位置按下标注,则标注将被解除并且将显示新标注.
// TPMapAnnotationView.m
@implementation TPMapAnnotationView
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if(selected)
{
TPMapAnnotation* anno = ((TPMapAnnotation*)self.annotation);
QuickInfoView* qi = [[QuickTabView alloc] initWithFrame:CGRectMake(0, 0, 440, 300)];
[qi displayDataForAnnotation:anno];
[self addSubview:qi];
// some animiation code that doesn't change things either way
}
else
{
[[self.subviews objectAtIndex:0] removeFromSuperview];
}
}
Run Code Online (Sandbox Code Playgroud)
下面的代码创建了TPMapAnnotationView.
// this is in the view controller that contains the MKMapView
- (MKAnnotationView *) …Run Code Online (Sandbox Code Playgroud) 默认情况下,我们可以在地图视图中显示用户的位置.点击注释引脚时,它将显示"当前位置".但我想将用户的地址显示为街道,城市和国家.我怎么能用CLGeoCoder课来做呢?
我正在尝试使用MapKit映射JSON数组.我可以使用下面的代码在地图上获得一个点,但是我需要标记几十个引脚,并且我准备了一个JSON数组.我的单点代码如下.
在我的.h文件中:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController {
MKMapView *mapView;
NSData *data;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@end
Run Code Online (Sandbox Code Playgroud)
在我的.m文件中:
NSData *data = @"[{"id":"1","name":"Jiffy Lube","lat":"21.306","lon":"-157.861"},
{"id":"2","name":"Bills
Oil","lat":"21.301","lon":"-157.863"},{"id":"3","name":"Auto Zone","lat":"21.307","lon":"-
157.862"}]";
// parse the JSON into a NSArray
NSError *error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
Run Code Online (Sandbox Code Playgroud) 我正在尝试将图像添加到我的MKAnnotation中.我有下面的代码.我该怎么把@"map_icon.png"添加到这个?任何帮助都会很棒.谢谢!
mapView.m文件:
// retrieve latitude and longitude from the dictionary entry
location.latitude = [dictionary[@"placeLatitude"] doubleValue];
location.longitude = [dictionary[@"placeLongitude"] doubleValue];
// create the annotation
newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"name"]
andCoordinate:location];
Run Code Online (Sandbox Code Playgroud)
MapViewAnnotation.m文件
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d
{
title = ttl;
coordinate = c2d;
return self;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我只是不知道如何计算MKMapView上的区域.有谁解决了这个问题呢?
这是我的代码,但它返回的方式太多了:
func ringArea() -> Double{
var area: Double = 0
if templocations.count > 2 {
var p1,p2:CLLocationCoordinate2D
for var i = 0; i < templocations.count - 1; i++ {
var loc = templocations[i] as CLLocation
p1 = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
loc = templocations[i+1] as CLLocation
p2 = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
var sinfunc: Float = (2 + sinf(Float(degreeToRadiant(p1.latitude))) + sinf(Float(degreeToRadiant(p2.latitude))))
area += degreeToRadiant(p2.longitude - p1.longitude) * Double(sinfunc)
}
area = area * kEarthRadius * kEarthRadius / 2;
} …Run Code Online (Sandbox Code Playgroud) 在我的iOS应用程序中,我有CLLocationCoordinate2D一个用户想要到达的地方的纬度和经度().我想要的是,当按下相对按钮时,将启动地图应用程序并启动到该地点的街道导航.我怎样才能做到这一点?我的代码到目前为止:
@IBAction func launchMapsApp(sender:UIButton) {
if (sender == self.navButton) {
let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: self.currentCoordinates, addressDictionary: nil))
mapItem.name = ""
//You could also choose: MKLaunchOptionsDirectionsModeWalking
let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: true]
mapItem.openInMapsWithLaunchOptions(launchOptions as? [String : AnyObject])
}
}
Run Code Online (Sandbox Code Playgroud)
但有了这个,地图应用程序只是启动,我只是看到我的州(意大利)的地图,没有更多的事情发生.也许,因为我只在模拟器中运行?谢谢大家
我有一个带注释的mapView,显示标题和副标题.字幕有时长于注释的宽度,所以我想知道我是否可以使它们成为多线?它到目前为止编码如此:
func annotate(newCoordinate, title: String, subtitle: String) {
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
annotation.title = title
annotation.subtitle = subtitle
self.map.addAnnotation(annotation)
}
Run Code Online (Sandbox Code Playgroud)
然后我有几个选项
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {...}
Run Code Online (Sandbox Code Playgroud)
这里没有关系.
是否可以制作自定义注释视图?我尝试了几件事,但没有任何效果.我能得到的最接近的是添加一个按钮来分别显示较长的字幕,但我宁愿把它放在注释中.
可能吗?
如何从(userLocation.swift)中的一个类(GPSLocation)获取定位结果,并在文件(target.swift)中的另一个类中使用它们?
我需要以下国家/地区的国家/地区代码,城市,经度和纬度:
userLocation.swift
import UIKit
import MapKit
class GPSLocation {
func getGPSLocation(completion: () -> Void) {
let locManager = CLLocationManager()
var currentLocation: CLLocation!
locManager.desiredAccuracy = kCLLocationAccuracyBest
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways) {
currentLocation = locManager.location
let latitude = String(format: "%.7f", currentLocation.coordinate.latitude)
let longitude = String(format: "%.7f", currentLocation.coordinate.longitude)
let location = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
fetchCountryAndCity(location: location) { countryCode, city in
// debugPrint("Country:", countryCode)
// debugPrint("City:", city)
}
// debugPrint("Latitude:", latitude)
// debugPrint("Longitude:", longitude)
}
}
//MARK: Find …Run Code Online (Sandbox Code Playgroud) mapkit ×10
ios ×9
iphone ×4
swift ×4
objective-c ×3
google-maps ×2
mkmapview ×2
xcode ×2
clgeocoder ×1
geolocation ×1
mkannotation ×1
uiview ×1