iOS6地图“查看路线”、导航功能的实现

在iOS6之前,苹果自带的是Google地图,所以“查看路线”的功能可以通过访问google的url来实现:

url 格式为:“http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f”

 

不过iOS6之后,苹果使用了自家地图,以上的方式就只能跳到google 地图网页版,不过体验不太好,

还是希望调用本地应用,于是找到了官方文档:

http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html

 

照文档描述,iOS6中的调用应该为:

访问url:“http://maps.apple.com/maps?saddr=%f,%f&daddr=%f,%f”

不过会提示服务器不可用,估计后面苹果又做更改了。

 

后来,找到网友的一篇文章,http://autumnice.blog.163.com/blog/static/5552002012912113247614/

在iOS6中调用地图API的方式可以实现,贴代码如下:

iOS6地图“查看路线”、导航功能的实现iOS6地图“查看路线”、导航功能的实现View Code

if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0) { // ios6以下,调用google map NSString *theString = [NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",settings.update_latitude,settings.update_longitude, __latitude, __longitude]; theString = [theString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; NSURL *url = [[[NSURL alloc] initWithString:theString] autorelease]; [[UIApplication sharedApplication] openURL:url]; } else { // 直接调用ios自己带的apple map  CLLocationCoordinate2D to; to.latitude = __latitude; to.longitude = __longitude; MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation]; MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[[MKPlacemark alloc] initWithCoordinate:to addressDictionary:nil] autorelease]]; toLocation.name = @"Destination"; [MKMapItem openMapsWithItems:[NSArray arrayWithObjects:currentLocation, toLocation, nil] launchOptions:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeDriving, [NSNumber numberWithBool:YES], nil] forKeys:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeKey, MKLaunchOptionsShowsTrafficKey, nil]]]; [toLocation release]; }

 

——————

如果不想使用苹果自带的地图的话,也可以使用第三方的地图,如百度;

百度地图的URI API:http://developer.baidu.com/map/uri-intro.htm#idmykey2

使用如下:“

baidumap://map/direction?origin=latlng:34.264642646862,108.95108518068|name:我家&destination=大雁塔&mode=driving&region=西安
//调起百度PC或Web地图,展示“西安市”从(34.264642646862,108.95108518068 )“我家”到“大雁塔”的驾车路线。

 

其他的如高德、搜狗、图吧等没有找到对应的URI API。

注意调用前,需要判断是否已安装百度地图:

使用 if ([[UIApplicationsharedApplication] canOpenURL:[NSURLURLWithString:@”baidumap://map/”]]){}

原文链接:https://www.cnblogs.com/zhulin/archive/2012/11/09/2761926.html

原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/32248

(0)
优速盾-小U的头像优速盾-小U
上一篇 2025年9月7日 17:13
下一篇 2025年9月7日 21:45

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

优速盾注册领取大礼包www.cdnb.net
/sitemap.xml