今天有个外卖的app要加导航去商户地址的功能,就简单的写了下调用目前用的比较广泛的三个地图的一键导航,还是一如既往的简单,github上又完善了一下功能:高德地图自定义大头针和气泡,点击气泡按钮导航
demo已上传到github上:https://github.com/15294871542/EasyMapNavi
//百度地图 文档地址:http://lbsyun.baidu.com/index.php?title=uri/api/ios
//腾讯地图 文档地址:http://lbs.qq.com/uri_v1/guide-route.html(前面的qqmap://需换一下表示app调用)
//高德地图 文档地址:http://lbs.amap.com/api/amap-mobile/gettingstarted
需要注意的就是需要在info.plist里面(LSApplicationQueriesSchemes)添加这三个地图的scheme:百度:baidumap 腾讯:qqmap 高德:iosamap
直接上关键代码,参数什么的可以去github或者下面的地图文档上看,另外调用的时候需要先判断手机上是否安装了要用的地图,在这儿我就不粘代码了,demo里也有。
//苹果手机自带地图-(void)iphoneMap:(NSDictionary*)dic
{//起点CLLocationCoordinate2D from =CLLocationCoordinate2DMake([dic[@"start_lat"] doubleValue],[dic[@"start_lng"] doubleValue]);
MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:from addressDictionary:nil]];
currentLocation.name=dic[@"start_address"];//终点CLLocationCoordinate2D to =CLLocationCoordinate2DMake([dic[@"end_lat"] doubleValue],[dic[@"end_lng"] doubleValue]);
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:to addressDictionary:nil]];
toLocation.name= dic[@"end_address"];NSArray*items = [NSArrayarrayWithObjects:currentLocation, toLocation,nil];NSDictionary*options =@{
MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey:
[NSNumbernumberWithInteger:MKMapTypeStandard],
MKLaunchOptionsShowsTrafficKey:@YES
};//打开苹果自身地图应用[MKMapItem openMapsWithItems:items launchOptions:options];
}//百度地图-(void)baiduMap:(NSDictionary*)dic
{NSString* urlString=[[NSStringstringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%@,%@|name:%@&mode=driving&coord_type=gcj02&src=webapp.navi.wanglu.etravel",dic[@"end_lat"],dic[@"end_lng"],dic[@"end_address"]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];
}//腾讯地图-(void)tencentMap:(NSDictionary*)dic
{NSString* urlString=[[NSStringstringWithFormat:@"qqmap://map/routeplan?type=drive&from=我的位置&tocoord=%@,%@&to=%@&policy=1",dic[@"end_lat"],dic[@"end_lng"],dic[@"end_address"]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];
}//高德地图-(void)gaodeMap:(NSDictionary*)dic
{NSURL* url = [NSURLURLWithString:[NSStringstringWithFormat:@"iosamap://navi?sourceApplication=etravel&backScheme=etravel&lat=%@&lon=%@&dev=0&style=2",dic[@"end_lat"],dic[@"end_lng"]]];if([[UIDevice currentDevice].systemVersionintegerValue] >=10) {//iOS10以后,使用新API[[UIApplicationsharedApplication] openURL:url options:@{} completionHandler:^(BOOLsuccess) {NSLog(@"scheme调用结束");
}];
}else{//iOS10以前,使用旧API[[UIApplicationsharedApplication] openURL:url];
}
}