如何调用带有当前位置的路线的iPhone地图作为起始地址 [英] How to invoke iPhone Maps for Directions with Current Location as Start Address

查看:88
本文介绍了如何调用带有当前位置的路线的iPhone地图作为起始地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以通过在带有参数 saddr 的谷歌地图URL上调用 openURL 来启动iPhone地图应用程序。和 daddr 位置字符串或Lat / Long(请参阅下面的示例)。



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

例如:

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

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

解决方案

Pre iOS 6



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



CLLocationCoordinate2D currentLocation = [self getCurrentLocation];
//这将使用目的地址。可以用%f,%f格式
来使用lat / long 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。经度,
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

最后,如果您确实要避免使用CoreLocation来显式查找当前位置并想要使用 @http://maps.google.com/maps?saddr=Current+Location&daddr=%@网址,然后请参阅下面的评论中提供的此链接,了解如何本地化当前位置字符串。但是,您正在利用另一个未公开的功能,正如Jason McCreary指出的那样,它在未来的发行版中可能无法可靠运行。




适用于iOS 6的更新



最初,地图使用了Google地图,但现在Apple和Google都有单独的地图应用。

1)如果您希望使用Google地图应用进行路由,请使用 comgooglemaps网址方案


$ b

 BOOL open = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; 

2)要使用Apple Maps,您可以使用新的 MKMapItem class for iOS 6. 请参阅Apple API文档



基本上,如果路由到目标坐标 latlong ):

MKPlacemark * place = [[MKPlacemark alloc] initWithCoordinate:latlong addressDictionary:nil];
MKMapItem * destination = [[MKMapItem alloc] initWithPlacemark:place];
destination.name = @姓名在这里!;
NSArray * items = [[NSArray alloc] initWithObjects:destination,nil];
NSDictionary * options = [[NSDictionary alloc] initWithObjectsAndKeys:
MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsDirectionsModeKey,nil];
[MKMapItem openMapsWithItems:items launchOptions:options];

为了在同一代码中同时支持iOS 6+和iOS 6,我建议使用类似苹果在 MKMapItem API文档页面上的代码


$ b

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

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


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).

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.

For example:

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

Except with something to invoke the current location bookmark in place of myLatLong.

解决方案

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]];

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.


Update for iOS 6

Originally, Maps used Google maps, but now, Apple and Google have separate maps apps.

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) To use Apple Maps, you can use the new MKMapItem class for iOS 6. See the Apple API docs here

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];

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
}

This would assume that your Xcode Base SDK is iOS 6 (or Latest iOS).

这篇关于如何调用带有当前位置的路线的iPhone地图作为起始地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆