如何调用 iPhone 地图以获取当前位置作为起始地址的路线 [英] How to invoke iPhone Maps for Directions with Current Location as Start Address
问题描述
我知道可以通过在带有位置字符串的参数 saddr
和 daddr
的谷歌地图 URL 上调用 openURL
来启动 iPhone 地图应用程序或纬度/经度(见下面的例子).
I know it's possible to start the iPhone maps application by calling openURL
on a google maps URL with parameters saddr
and daddr
with location strings or Lat/Long (see example below).
但我想知道是否可以将起始地址设为 当前位置" 地图书签,以便我可以使用 地图 应用的位置处理代码.我的 Google 搜索毫无结果.
But I'm wondering if it's possible to make the start address be the "Current Location" maps bookmark so that I can use the Maps app's location handling code. My Google search has been pretty fruitless.
例如:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%@&daddr=%@", myLatLong, latlong]]];
除了调用当前位置书签代替myLatLong
.
Except with something to invoke the current location bookmark in place of myLatLong
.
推荐答案
Pre iOS 6
您需要使用 Core Location 来获取当前位置,但使用该纬度/经度对,您可以获得地图以将您从那里路由到街道地址或位置.像这样:
Pre iOS 6
You need to use Core Location to get the current location, but with that lat/long pair, you can get Maps to route you from there, to a street address or location. Like so:
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,然后 参见我在下面的评论中提供了此链接,以了解如何本地化 Current+Location 字符串.但是,您正在利用另一个未记录的功能,正如 Jason McCreary 在下面指出的那样,它在未来的版本中可能无法可靠地工作.
Finally, if you do want to avoid using CoreLocation to explicitly find the current location, and want to use the @"http://maps.google.com/maps?saddr=Current+Location&daddr=%@"
url instead, then see this link that I provided in comments below for how to localize the Current+Location string. However, you are taking advantage of another undocumented feature, and as Jason McCreary points out below, it may not work reliably in future releases.
最初,Maps 使用 Google 地图,但现在,Apple 和 Google 拥有独立的地图应用.
Originally, Maps used Google maps, but now, Apple and Google have separate maps apps.
1) 如果您希望使用 Google 地图应用进行路线规划,请使用 comgooglemaps 网址方案:
1) If you wish to route using the Google Maps app, use the comgooglemaps URL scheme:
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 文档
2) To use Apple Maps, you can use the new MKMapItem
class for iOS 6. See the Apple API docs here
基本上,如果路由到目的地坐标 (latlong
),您将使用类似的方法:
Basically, you will use something like this, if routing to destination coordinates (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 文档页面上提供的代码:
In order to support both iOS 6+ and pre iOS 6 in the same code, I'd recommend using something like this code that Apple has on the MKMapItem
API doc page:
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).
This would assume that your Xcode Base SDK is iOS 6 (or Latest iOS).
这篇关于如何调用 iPhone 地图以获取当前位置作为起始地址的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!