2012-09-14 21 views
8

这是一个奇怪的问题:我的应用程序应该能够调用内置的地图在iOS中(包括5.1和6)。事实证明,它在iOS6下工作得很好,但不在iOS5.1下。 iOS6中的地图被调用,并且从saddr到daddr的方向被跟踪,但是当我在iOS5中时,地图应用被调用,但只有一个针被放在daddr上。由于某些未知的原因,初始坐标(saddr)没有显示,也没有追踪方向。电话地图从您的应用程序内的路线 - iOS5的iOS6的

这里是我的代码:

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

我试图改变“http://maps.google.com/something”,但它调用Safari浏览器,而不是建立在地图应用程序的URL。我注意到这些变量正在被正确地传递给URL。

任何想法?

在此先感谢!

+0

几乎相同的问题[这个旧的一个(http://stackoverflow.com/q/576768/119114),除了起始地址是当前位置中的其他问题(并没有真正发生大的变化的问题)。 – Nate

回答

35

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

//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。

我认为最好在iOS6中使用openInMapsWithLaunchOptions,因为它可以让您完全控制地图应用程序的响应方式。

+1

感谢您的帮助!我遵循你的建议,并使用openInMapsWithLaunchOptions,它的工作完美。我使用的唯一技巧是将当前位置作为参数传递给NSString * url = [NSString stringWithFormat:@“http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f” ,currentLatitude,currentLongitude,纬度,经度];因为我无法获得Current + Location sintax的工作。也许它在iOS 5.1.1中有一些关于沙盒的事情。 – lsp

+0

@lsp,使用'Current + Location'的问题在我的答案[与这个非常相似的旧问题](http://stackoverflow.com/a/964131/119114)中讨论,以及响应它的意见。 – Nate

+0

我不希望地图视图覆盖整个屏幕, 它应该只显示我的屏幕的一半是可能的。 – Arun

0

您可以使用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常量。

相关问题