最近收到了这么一个需求,就是支持目前主流的地图APP导航,也就是说跳转至第三方应用。说实在的一开始我是拒绝的,不过最后还是做了,全是体力活,为了方便大家,我就终结一下,以供参考:
首先是判断APP是否安装代码如下:
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]
然后就是添加各个地图APP的白名单:
百度地图:baidumap:
高德地图:iosamap:
腾讯地图:qqmap:
谷歌地图:comgooglemapsurl:
系统地图就不用这么麻烦了,直接这样就好:
CLLocationCoordinate2D endCoor =坐标;
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];
toLocation.name = [NSString stringWithFormat:@"到 %@", 目的地];
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
其他地图的打开如下:
百度地图:
NSString *stringURL = [NSString stringWithFormat:@"baidumap://map/direction?origin=%f,%f&destination=%f,%f&mode=driving",user.userLocCoord2D.latitude, user.userLocCoord2D.longitude,
目的地.latitude, 目的地.longitude];
[[UIApplication sharedApplication] openURL:url]
高德地图:
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&backScheme=%@&sname=%@&dname=%@&dev=0&m=0&t=0&sid=BGVIS1&did=BGVIS2&dlat=%lf&dlon=%lf",@"APP名称", @"iosamap", @"我的位置",目的地,endCoor.latitude, endCoor.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
腾讯地图:
NSString *urlString =[[NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&from=我的位置&to=%@&tocoord=%lf,%lf&policy=1&referer=tengxun",目的地,endCoor.latitude,endCoor.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
谷歌地图:(国内谷歌地图无法访问得要轮子哦)
NSString *urlString = [[NSString stringWithFormat:@"comgooglemapsurl://www.google.com/maps/preview/@%lf,%lf,6z",endCoor.latitude, endCoor.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
以上就是了,至于对应的链接,当时忘记记录了,建议使用谷歌搜一下,都可以看到。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)