需求:由App跳转至本机地图软件店铺位置实现导航
参考文章:https://www.jianshu.com/p/de16b81363e8
一、白名单添加
一、target -> info
1、添加key :LSApplicationQueriesSchemes,type选array
2、添加item:baidumap,iosamap,comgooglemaps,qqmap
二、接收后台店铺名和经纬度
CLLocationCoordinate2D coordinate;
coordinate.latitude = 39.9065759877;
coordinate.longitude = 116.3987731934;
//不传地理位置时可以用下面的设置来判断
//没有地理位置会根据名字弹出选择目的地列表,有地理位置以地理位置为准
//coordinate.latitude = 0;
//coordinate.longitude = 0;
三、点击事件 查看地图
[self.maps removeAllObjects];
[self.maps addObjectsFromArray: [self getInstalledMapAppWithAddressStr:@"学校" withEndLocation:coordinate]];
[self showAmaps:coordinate];
四、判断手机安装的地图软件
高德地图:文档
百度地图:文档
腾讯地图:文档 该文档为可使用的web端的调用连接,文档上方有移动端的调用地址:“导航/路线规划”,不过需要开发者key.
谷歌地图:网络不行 啥也不显示 可能需要VPN。文档 可能需要翻墙看
- (NSArray *)getInstalledMapAppWithAddressStr:(NSString *)addrString withEndLocation:(CLLocationCoordinate2D)endLocation
{
NSMutableArray *maps = [NSMutableArray array];
//苹果地图
NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];
iosMapDic[@"title"] = @"苹果地图";
[maps addObject:iosMapDic];
NSString *appStr = NSLocalizedString(@"app_name", nil);
//高德地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
gaodeMapDic[@"title"] = @"高德地图";
NSString *urlString;
if(endLocation.latitude != 0 && endLocation.latitude != 0){
urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&sid=BGVIS1&did=BGVIS2&dname=%@&dlat=%@&dlon=%@&dev=0&t=2",appStr ,addrString,@(endLocation.latitude),@(endLocation.longitude)] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}else{
urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&sid=BGVIS1&did=BGVIS2&dname=%@&dev=0&t=2",appStr ,addrString] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}
gaodeMapDic[@"url"] = urlString;
[maps addObject:gaodeMapDic];
}
//百度地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];
baiduMapDic[@"title"] = @"百度地图";
NSString *urlString;
if(endLocation.latitude != 0 && endLocation.latitude != 0){
urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=我的位置&destination=name:%@|latlng:%@,%@&mode=walking&src=%@&coord_type=gcj02",addrString,@(endLocation.latitude),@(endLocation.longitude),appStr] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}else{
urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=我的位置&destination=%@&mode=walking&src=%@",addrString,appStr] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}
baiduMapDic[@"url"] = urlString;
[maps addObject:baiduMapDic];
}
//腾讯地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];
qqMapDic[@"title"] = @"腾讯地图";
NSString *urlString;
if(endLocation.latitude != 0 && endLocation.latitude != 0){
urlString =[ [NSString stringWithFormat:@"qqmap://map/routeplan?type=walk&from=我的位置&tocoord=%f,%f&to=%@&coord_type=1&policy=0&referer=%@",endLocation.latitude , endLocation.longitude ,addrString,appStr] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
qqMapDic[@"url"] = urlString;
[maps addObject:qqMapDic];
}else{
urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?type=walk&from=我的位置&to=%@&coord_type=1&policy=0&referer=%@",addrString,appStr] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
// qqMapDic[@"url"] = urlString;
//
// [maps addObject:qqMapDic];
}
}
//谷歌地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
NSMutableDictionary *googleMapDic = [NSMutableDictionary dictionary];
googleMapDic[@"title"] = @"谷歌地图";
NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?saddr=&daddr=%@&directionsmode=walking",addrString] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
googleMapDic[@"url"] = urlString;
[maps addObject:googleMapDic];
}
return maps;
}
五、展示支持的 地图软件
- (void)showAmaps:(CLLocationCoordinate2D)gps
{
if (self.maps.count == 0) {
return;
}
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
for (int i = 0; i < self.maps.count; i++) {
if (i == 0) {
[alertVC addAction:[UIAlertAction actionWithTitle:self.maps[i][@"title"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self navAppleMap:gps];
}]];
}else{
[alertVC addAction:[UIAlertAction actionWithTitle:self.maps[i][@"title"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self otherMap:i];
}]];
}
}
[alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alertVC animated:YES completion:nil];
}
// 苹果地图
- (void)navAppleMap:(CLLocationCoordinate2D)gps
{
MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:gps addressDictionary:nil]];
NSArray *items = @[currentLoc,toLocation];
NSDictionary *dic = @{
MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeWalking,
MKLaunchOptionsMapTypeKey: @(MKMapTypeStandard),
MKLaunchOptionsShowsTrafficKey: @(YES)
};
[MKMapItem openMapsWithItems:items launchOptions:dic];
}
/// 第三方地图
- (void)otherMap:(NSInteger)index
{
NSDictionary *dic = self.maps[index];
NSString *urlString = dic[@"url"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
网友评论