如何使用当前位置调用iPhone Maps for Directions作为起始地址

我知道可以通过在带有位置string或纬度/ daddr参数saddrdaddr的谷歌地图URL(参见下面的示例)上调用openURL来启动iPhone地图应用程序。

但是我想知道是否可以使起始地址成为“当前位置”地图书签,以便我可以使用地图应用程序的位置处理代码。 我的谷歌search一直没有结果。

例如:

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%@&daddr=%@", myLatLong, latlong]]]; 

除了调用当前位置书签来代替myLatLong

预装iOS 6

您需要使用核心位置获取当前位置,但是使用该纬度/长度对,您可以使用地图将您从那里路由到街道地址或位置。 像这样:

 CLLocationCoordinate2D currentLocation = [self getCurrentLocation]; // this uses an address for the destination. can use lat/long, too with %f,%f format NSString* address = @"123 Main St., New York, NY, 10001"; NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%@", currentLocation.latitude, currentLocation.longitude, [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]]; 

最后,如果您确实要避免使用CoreLocation来显式查找当前位置,并且想要使用@"http://maps.google.com/maps?saddr=Current+Location&daddr=%@" url, 请参阅这个链接,我在下面的评论中提供了如何本地化当前+位置string。 但是,您正在利用另一个无证的function,正如Jason McCreary指出的那样,在未来的版本中可能无法可靠地工作。


更新iOS 6

最初, 地图使用谷歌地图,但现在,苹果和谷歌有单独的地图应用程序。

1)如果您希望使用Google地图应用程序进行路线,请使用comgooglemapsurlscheme :

 NSString* url = [NSString stringWithFormat: @"comgooglemaps://?daddr=%@&directionsmode=driving", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; BOOL opened = [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]]; 

2)要使用Apple Maps,可以使用iOS 6的新MKMapItem类。 请参阅Apple API文档

基本上,如果路由到目的地坐标latlong ),你将使用这样的东西:

  MKPlacemark* place = [[MKPlacemark alloc] initWithCoordinate: latlong addressDictionary: nil]; MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark: place]; destination.name = @"Name Here!"; NSArray* items = [[NSArray alloc] initWithObjects: destination, nil]; NSDictionary* options = [[NSDictionary alloc] initWithObjectsAndKeys: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsDirectionsModeKey, nil]; [MKMapItem openMapsWithItems: items launchOptions: options]; 

为了在相同的代码中同时支持iOS 6+和iOS 6,我推荐使用Apple在MKMapItem API文档页面上使用的代码。

 Class itemClass = [MKMapItem class]; if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { // iOS 6 MKMapItem available } else { // use pre iOS 6 technique } 

这将假设您的Xcode Base SDK是iOS 6(或最新的iOS )。

 NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=Current Location&saddr=%@",startAddr]; NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [[UIApplication sharedApplication] openURL:url]; [url release]; 

这只适用于iPhone / iPod语言设置为英语的情况。 如果您想支持其他语言,则必须使用本地化string来匹配地图书签名称。

这适用于iPhone:

http://maps.google.com/maps?saddr=Current Location&daddr = 123 Main St,Ottawa,ON

您可以使用预处理器#define,如:

 #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) 

了解你的iOS版本。 然后,我也可以使用这个代码来支持iOS 6:

 NSString* addr = nil; if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude]; } else { addr = [NSString stringWithFormat:@"http://maps.apple.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude]; } NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [[UIApplication sharedApplication] openURL:url]; 

由于沙盒,您无权访问地图应用程序的书签。

而是使用核心位置自己确定当前位置。 然后在您构build的URL中使用该位置(纬度和长度)来打开地图。

我build议查看一下CMMapLauncher ,这是我构build的一个小型库,用于通过特定的映射请求启动Apple,Google和其他iOS映射应用程序。 使用CMMapLauncher,在你的问题中得到方向的代码是:

 [CMMapLauncher launchMapApp:CMMapAppAppleMaps forDirectionsFrom:[CMMapPoint mapPointWithName:@"Origin" coordinate:myLatLong] to:[CMMapPoint mapPointWithName:@"Destination" coordinate:latlong]]; 

如您所见,它还封装了iOS 6和其他版本之间所需的版本检查。

嘿,因为iOS6已经不在了!

苹果公司做了一些不寻常的事情(从我的观点来看)。

苹果的地图已经启动,对于运行iOS 6的设备,如果你想让iDevice打开原生的Plan应用程序,你不应该使用maps.google.com/?q= 。 现在它将是maps.apple.com/?q=

为了让开发人员不需要太多工作,友好的maps.apple.com服务器将所有非Apple设备redirect到maps.google.com,以便更改是透明的。

这样我们开发人员只需要将所有的谷歌查询string切换到苹果的。 这是我不喜欢的东西。

我今天必须实现这个function,所以我做到了。 但是我觉得我不应该把手机网站上的每一个url都改写成苹果的地图服务器,所以我想我只需要检测iDevices服务器端,并为这些服务器提供苹果url。 我以为我会分享。

我正在使用PHP,所以我使用了开源Mobile Detect库: http : //code.google.com/p/php-mobile-detect/

只要使用isiPad伪方法作为一个布尔getter,你就完成了,你不会将谷歌转换成苹果;-)

 $server=$detect->isiPad()?"apple":"google"; $href="http://maps.{$server}.com/?q=..." 

干杯!

您现在只能通过移动设备上的url在html中执行此操作。 这是一个例子 。 很酷的是,如果你把它变成一个二维码,你可以通过在手机上扫描的方式从任何地方获取方向。

真正的解决scheme可以在这里find。 Z5概念iOS开发代码片段

它只需要一点点的编码。

 - (IBAction)directions1_click:(id)sender { NSString* address = @"118 Your Address., City, State, ZIPCODE"; NSString* currentLocation = @"Current Location"; NSString* url = [NSStringstringWithFormat: @"http://maps.google.com/maps?saddr=%@&daddr=%@",[currentLocation stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; UIApplication *app = [UIApplicationsharedApplication]; [app openURL: [NSURL URLWithString: url]]; } 

对于iOS6,苹果文档推荐使用等效的maps.apple.com URL Scheme

所以使用

 http://maps.apple.com/maps?saddr=%f,%f&daddr=%f,%f 

代替

 http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f 

代码是向后兼容的

  NSString* versionNum = [[UIDevice currentDevice] systemVersion]; NSString *nativeMapScheme = @"maps.apple.com"; if ([versionNum compare:@"6.0" options:NSNumericSearch] == NSOrderedAscending) nativeMapScheme = @"maps.google.com"; } NSString* url = [NSString stringWithFormat: @"http://%@/maps?saddr=%f,%f&daddr=%f,%f", nativeMapScheme startCoordinate.latitude, startCoordinate.longitude, endCoordinate.latitude, endCoordinate.longitude]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; 

苹果地图URLscheme中包含其他支持的参数: 苹果URLscheme参考

如果在代码的其他部分有条件代码,则可以使用这些iOS版本检测macros。 iOS版本的macros

如果您不想要求位置权限并且没有lat和lng,请使用以下内容。

 NSString *destinationAddress = @"Amsterdam"; Class itemClass = [MKMapItem class]; if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:destinationAddress completionHandler:^(NSArray *placemarks, NSError *error) { if([placemarks count] > 0) { MKPlacemark *placeMark = [[MKPlacemark alloc] initWithPlacemark:[placemarks objectAtIndex:0]]; MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:placeMark]; MKMapItem *mapItem2 = [MKMapItem mapItemForCurrentLocation]; NSArray *mapItems = @[mapItem, mapItem2]; NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES }; [MKMapItem openMapsWithItems:mapItems launchOptions:options]; } else { //error nothing found } }]; return; } else { NSString *sourceAddress = [LocalizedCurrentLocation currentLocationStringForCurrentLanguage]; NSString *urlToOpen = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@", [sourceAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [destinationAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlToOpen]]; } 

对于ios5,当前位置需要使用正确的语言。 我从这个post使用LocalizedCurrentLocation http://www.martip.net/blog/localized-current-location-string-for-iphone-apps

对于ios6,我使用CLGeocoder来获取地标,然后用它和当前位置打开地图。

请记住添加CoreLocation.framework和MapKit.framework

对于iOS6地图应用,您可以使用上面发布的相同urlhttp://maps.google.com/maps?saddr=%f,%f&daddr=%@

但是不使用Google地图url,而是使用带有maps://的urlmaps://导致以下url: maps://saddr=%f,%f&daddr=%@.

使用“当前位置”似乎不起作用,所以我留在坐标。

Antother好东西:向后兼容:在iOS5上,它启动了Google Maps应用程序。

使用当前版本的Google地图,只需省略sadr参数:

saddr :…如果该值留空,则将使用用户的当前位置。

https://developers.google.com/maps/documentation/ios/urlscheme

我的build议是使用OpenInGoogleMaps-iOS,因为这是一个最新的select(截至2015年11月),它支持cocoa豆荚安装,你只需点击几下即可。

安装使用: pod "OpenInGoogleMaps"

需要在头文件中使用: #import "OpenInGoogleMapsController.h"

下面的示例代码:

 /*In case the user does NOT have google maps, then apple maps shall open*/ [OpenInGoogleMapsController sharedInstance].fallbackStrategy = kGoogleMapsFallbackAppleMaps; /*Set the coordinates*/ GoogleMapDefinition *definition = [[GoogleMapDefinition alloc] init]; CLLocationCoordinate2D metsovoMuseumCoords; //coordinates struct metsovoMuseumCoords.latitude = 39.770598; metsovoMuseumCoords.longitude = 21.183215; definition.zoomLevel = 20; definition.center = metsovoMuseumCoords; //this will be the center of the map /*and here we open the map*/ [[OpenInGoogleMapsController sharedInstance] openMap:definition]; 

我用不同的线索回答了这个问题。 ( 当前位置不适用于Apple Maps IOS 6 )。 您需要先获取当前位置的坐标,然后使用它来创build地图url。

如果您不提供源位置,则会以当前位置为源。 尝试下面的代码 –

let urlString =“ http://maps.apple.com/maps?daddr=(destinationLocation.latitude),(destinationLocation.longitude)&dirflg=d ”}

UIApplication.shared.openURL(URL(string:urlString)!)