-->

调用地图从您的应用程序内的路线 - iOS5的iOS6的(Call Maps for direct

2019-07-31 13:38发布

这是一个奇怪的问题:我的应用程序应该能够调用内置的地图在iOS中(包括5.1和6)。 原来,它的工作原理iOS5.1下下iOS6的但不就好了。 在iOS6的地图被称为和SADDR到DADDR方向被跟踪,但是当我在iOS5的地图应用程序的调用,但只是一个引脚将在DADDR放。 一些未知的原因的初始坐标(SADDR)没有显示,没有方向被追踪。

这里是我的代码:

addr = [NSString stringWithFormat: @"maps://saddr=%f,%f&daddr=%f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude, oldLatitude, oldLongitude];
NSURL *url = [NSURL URLWithString:addr];
[[UIApplication sharedApplication] openURL:url];

我曾尝试更改URL为“http://maps.google.com/something”,但它调用Safari浏览器,而不是内置的地图应用程序。 我注意到这两个变量被正确地传递到URL。

有任何想法吗?

提前致谢!

Answer 1:

我有一个类似的问题,我不得不创造一些条件OS代码来处理的事实,谷歌地图应用程序已被移除。 从新MKMapItem参考

//first create latitude longitude object
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude,longitude);

//create MKMapItem out of coordinates
MKPlacemark* placeMark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
MKMapItem* destination =  [[MKMapItem alloc] initWithPlacemark:placeMark];

if([destination respondsToSelector:@selector(openInMapsWithLaunchOptions:)])
{
    //using iOS6 native maps app
    [destination openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}];        
} 
else
{
    //using iOS 5 which has the Google Maps application
    NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=Current+Location&daddr=%f,%f", latitude, longitude];
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
}

[placeMark release];
[destination release];

要获取步行路线:

  1. 对于iOS 6的地图-您可以设置MKLaunchOptionsDirectionsModeWalking而不是MKLaunchOptionsDirectionsModeDriving
  2. 对于谷歌地图-添加&dirflg=w的URL。

我认为这是最好使用openInMapsWithLaunchOptions在iOS6的,因为它可以让你的地图应用程序将如何作出反应的完全控制。



Answer 2:

您可以使用MKPlacemarkMKMapItem推出同时具有协调的地图应用地图图钉标题:

NSString *pinTitle;
CLLocationCoordinate2D coordinate;

MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:@{(id)kABPersonAddressStreetKey: pinTitle}];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];

if ([mapItem respondsToSelector:@selector(openInMapsWithLaunchOptions:)])
{
    [mapItem openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving}];
}
else
{
    // Google Maps fallback
    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%f,%f&saddr=Current+Location", locationItem.coordinate.latitude, locationItem.coordinate.longitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}

请注意,您必须要链接AddressBook.framework并添加#import <AddressBook/AddressBook.h>在你的代码某处利用的kABPersonAddressStreetKey不变。



文章来源: Call Maps for directions from inside your app - iOS5 iOS6