在iOS 6之前,打开这样的网址会打开(Google)地图应用:
NSURL *url = [NSURL URLWithString:@"http://maps.google.com/?q=New+York"];
[[UIApplication sharedApplication] openURL:url];
现在有了新的Apple Maps实现,这只会将Mobile Safari打开到Google地图。如何在iOS 6中实现相同的行为?如何以编程方式打开地图应用并将其指向特定位置/地址/搜索/其他?
答案 0 :(得分:281)
以下是官方的Apple方式:
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
// Create an MKMapItem to pass to the Maps app
CLLocationCoordinate2D coordinate =
CLLocationCoordinate2DMake(16.775, -3.009);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate
addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:@"My Place"];
// Pass the map item to the Maps app
[mapItem openInMapsWithLaunchOptions:nil];
}
如果您想要获取驾车或步行说明,可以在mapItemForCurrentLocation
的数组中添加MKMapItem
+openMapsWithItems:launchOptions:
,并正确设置启动选项。< / p>
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
// Create an MKMapItem to pass to the Maps app
CLLocationCoordinate2D coordinate =
CLLocationCoordinate2DMake(16.775, -3.009);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate
addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:@"My Place"];
// Set the directions mode to "Walking"
// Can use MKLaunchOptionsDirectionsModeDriving instead
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeWalking};
// Get the "Current User Location" MKMapItem
MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
// Pass the current location and destination map items to the Maps app
// Set the direction mode in the launchOptions dictionary
[MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem]
launchOptions:launchOptions];
}
您可以在else
之后的if
语句中保留原始iOS 5及更低版本的代码。请注意,如果您颠倒openMapsWithItems:
数组中项目的顺序,您将获得从坐标到当前位置的路线。您可以通过传递构造的MKMapItem
而不是当前位置图项来使用它来获取任意两个位置之间的路线。我没试过。
最后,如果您有想要路线的地址(作为字符串),请使用地理编码器通过MKPlacemark
创建CLPlacemark
。
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"Piccadilly Circus, London, UK"
completionHandler:^(NSArray *placemarks, NSError *error) {
// Convert the CLPlacemark to an MKPlacemark
// Note: There's no error checking for a failed geocode
CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc]
initWithCoordinate:geocodedPlacemark.location.coordinate
addressDictionary:geocodedPlacemark.addressDictionary];
// Create a map item for the geocoded address to pass to Maps app
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:geocodedPlacemark.name];
// Set the directions mode to "Driving"
// Can use MKLaunchOptionsDirectionsModeWalking instead
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
// Get the "Current User Location" MKMapItem
MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
// Pass the current location and destination map items to the Maps app
// Set the direction mode in the launchOptions dictionary
[MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem] launchOptions:launchOptions];
}];
}
答案 1 :(得分:80)
找到我自己的问题的答案。 Apple记录其地图网址格式here。看起来您基本上可以将maps.google.com
替换为maps.apple.com
。
更新:事实证明,iOS 6上的MobileSafari也是如此;点击指向http://maps.apple.com/?q=...
的链接可以使用该搜索打开地图应用,就像http://maps.google.com/?q=...
在之前版本中所做的那样。这有效,并记录在上面链接的页面中。
更新:这回答了我关于URL格式的问题。但是nevan king的回答here(见下文)是对实际Maps API的一个很好的总结。
答案 2 :(得分:41)
最好的方法是在MKMapItem
openInMapsWithLaunchOptions:launchOptions
上调用新的iOS 6方法
示例:
CLLocationCoordinate2D endingCoord = CLLocationCoordinate2DMake(40.446947, -102.047607);
MKPlacemark *endLocation = [[MKPlacemark alloc] initWithCoordinate:endingCoord addressDictionary:nil];
MKMapItem *endingItem = [[MKMapItem alloc] initWithPlacemark:endLocation];
NSMutableDictionary *launchOptions = [[NSMutableDictionary alloc] init];
[launchOptions setObject:MKLaunchOptionsDirectionsModeDriving forKey:MKLaunchOptionsDirectionsModeKey];
[endingItem openInMapsWithLaunchOptions:launchOptions];
这将开始从当前位置开车导航。
答案 3 :(得分:7)
我看到你发现了maps.apple.com网址“方案”。这是一个不错的选择,因为它会自动将旧设备重定向到maps.google.com。但对于iOS 6,您可能希望利用以下新课程:MKMapItem。
您感兴趣的两种方法:
答案 4 :(得分:5)
这是一个使用在Swift中完成的nevan king的解决方案的课程:
class func openMapWithCoordinates(theLon:String, theLat:String){
var coordinate = CLLocationCoordinate2DMake(CLLocationDegrees(theLon), CLLocationDegrees(theLat))
var placemark:MKPlacemark = MKPlacemark(coordinate: coordinate, addressDictionary:nil)
var mapItem:MKMapItem = MKMapItem(placemark: placemark)
mapItem.name = "Target location"
let launchOptions:NSDictionary = NSDictionary(object: MKLaunchOptionsDirectionsModeDriving, forKey: MKLaunchOptionsDirectionsModeKey)
var currentLocationMapItem:MKMapItem = MKMapItem.mapItemForCurrentLocation()
MKMapItem.openMapsWithItems([currentLocationMapItem, mapItem], launchOptions: launchOptions)
}
答案 5 :(得分:4)
我发现使用http://maps.apple.com?q= ...链接设置首先在较旧的设备上打开Safari浏览器很烦人。
因此,对于iOS 5设备,通过引用maps.apple.com打开您的应用程序,步骤如下:
我认为(非常明显和令人困惑的)第2步和第3步令用户烦恼。 因此,我检查操作系统版本,并在设备上运行maps.google.com或maps.apple.com(对于分别为ios 5或ios 6 OS版本)。
答案 6 :(得分:3)
我对这个问题的研究使我得出以下结论:
答案 7 :(得分:3)
如果您想改为打开Google地图(或作为辅助选项提供),您可以使用comgooglemaps://
和comgooglemaps-x-callback://
记录的here网址。
答案 8 :(得分:3)
在启动url之前,从网址中删除任何特殊字符,并用+替换空格。这将为您省去一些麻烦:
NSString *mapURLStr = [NSString stringWithFormat: @"http://maps.apple.com/?q=%@",@"Limmattalstrasse 170, 8049 Zürich"];
mapURLStr = [mapURLStr stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSURL *url = [NSURL URLWithString:[mapURLStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL:url]){
[[UIApplication sharedApplication] openURL:url];
}
答案 9 :(得分:2)
NSString *address = [NSString stringWithFormat:@"%@ %@ %@ %@"
,[dataDictionary objectForKey:@"practice_address"]
,[dataDictionary objectForKey:@"practice_city"]
,[dataDictionary objectForKey:@"practice_state"]
,[dataDictionary objectForKey:@"practice_zipcode"]];
NSString *mapAddress = [@"http://maps.apple.com/?q=" stringByAppendingString:[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"Map Address %@",mapAddress);
[objSpineCustomProtocol setUserDefaults:mapAddress :@"webSiteToLoad"];
[self performSegueWithIdentifier: @"provider_to_web_loader_segue" sender: self];
// VKJ
答案 10 :(得分:2)
根据@ PJeremyMalouf的回答更新到Swift 4:
private func navigateUsingAppleMaps(to coords:CLLocation, locationName: String? = nil) {
let placemark = MKPlacemark(coordinate: coords.coordinate, addressDictionary:nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = locationName
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
let currentLocationMapItem = MKMapItem.forCurrentLocation()
MKMapItem.openMaps(with: [currentLocationMapItem, mapItem], launchOptions: launchOptions)
}
答案 11 :(得分:1)
不使用地图,只是以编程方式使用UiButton动作,这对我来说非常有用。
// Button triggers the map to be presented.
@IBAction func toMapButton(sender: AnyObject) {
//Empty container for the value
var addressToLinkTo = ""
//Fill the container with an address
self.addressToLinkTo = "http://maps.apple.com/?q=111 Some place drive, Oak Ridge TN 37830"
self.addressToLinkTo = self.addressToLinkTo.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let url = NSURL(string: self.addressToLinkTo)
UIApplication.sharedApplication().openURL(url!)
}
您可以稍微传播一些代码。例如,我将变量作为类级别变量,让另一个函数填充它,然后按下按钮时,只需获取变量中的内容并将其擦除即可在URL中使用。