如何从objective c在iPhone上启动地图应用程序

如何从objective c在iPhone上启动地图应用程序,iphone,objective-c,ios,ios6,Iphone,Objective C,Ios,Ios6,我为IOS 6开发应用程序。 我想运行“地图”应用程序并将其传递给“开始”和“目标”,以便我可以导航用户。 UIApplication *app = [UIApplication sharedApplication]; NSString *coordinates = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%f,%f&saddr=%f,%f", ...]; [app openURL:

我为IOS 6开发应用程序。
我想运行“地图”应用程序并将其传递给“开始”和“目标”,以便我可以导航用户。

UIApplication *app = [UIApplication sharedApplication];

    NSString *coordinates = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%f,%f&saddr=%f,%f", ...];

    [app openURL:[NSURL URLWithString: coordinates]];
我原以为这段代码会在模拟器上的浏览器中打开谷歌地图,在设备上打开地图应用程序,但在设备上它会运行浏览器谷歌地图。

我做错什么了吗?

如果你不知道,苹果不再使用谷歌地图,因此你必须使用他们新的苹果地图URL方案。 (注意:如果您支持iOS 5,那么您应该同时使用谷歌地图方案和苹果地图)

下面是一个示例查询
http://maps.apple.com/maps?daddr=San+弗朗西斯科,+CA&SADD=cupertino


这是它的文档:

如果你不知道,苹果不再使用谷歌地图,因此你必须使用他们新的苹果地图URL方案。 (注意:如果您支持iOS 5,那么您应该同时使用谷歌地图方案和苹果地图)

下面是一个示例查询
http://maps.apple.com/maps?daddr=San+弗朗西斯科,+CA&SADD=cupertino


以下是它的文档:

另一个选项,如果您有一个MKPlacemark对象:

// placemark is your MKPlacemark object
MKMapItem* destination =  [[MKMapItem alloc] initWithPlacemark:placemark];

if([destination respondsToSelector:@selector(openInMapsWithLaunchOptions:)])
{
   // Using iOS6 native maps app
   [destination openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}];      
}
else
{
   // Using iOS5 which has the Google Maps application
   NSString *currentLocation = @"Current%20Location";
   NSString *routeString = [NSString stringWithFormat:@"%@saddr=%@&daddr=%@", kMapsBaseUrl, currentLocation, address.mapAddress];
   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:routeString]];
}

另一个选项,如果您有一个MKPlacemark对象:

// placemark is your MKPlacemark object
MKMapItem* destination =  [[MKMapItem alloc] initWithPlacemark:placemark];

if([destination respondsToSelector:@selector(openInMapsWithLaunchOptions:)])
{
   // Using iOS6 native maps app
   [destination openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}];      
}
else
{
   // Using iOS5 which has the Google Maps application
   NSString *currentLocation = @"Current%20Location";
   NSString *routeString = [NSString stringWithFormat:@"%@saddr=%@&daddr=%@", kMapsBaseUrl, currentLocation, address.mapAddress];
   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:routeString]];
}
}

欲了解更多信息,请访问

}


有关更多信息,请访问

这是我在iOS 8上使用的

首先,我尝试打开url@“comgooglemaps://”,如果它有效,这意味着他们安装了谷歌地图应用程序,然后我可以打开该应用程序

如果不起作用,那么应用程序就不存在了,只需在Safari中打开谷歌地图

在这两种情况下,它都传递查询
q=London

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]){ //open google maps app
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"comgooglemaps://?q=London"]];
}
else{ //open browser 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]];
}

这是我在iOS 8上使用的

首先,我尝试打开url@“comgooglemaps://”,如果它有效,这意味着他们安装了谷歌地图应用程序,然后我可以打开该应用程序

如果不起作用,那么应用程序就不存在了,只需在Safari中打开谷歌地图

在这两种情况下,它都传递查询
q=London

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]){ //open google maps app
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"comgooglemaps://?q=London"]];
}
else{ //open browser 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]];
}