iOS APP内调起第三方地图

1.根据你手机上有没有安装这个地图应用,没有就不会出现。

- (BOOL)canOpenURL:(NSURL*)url NS_AVAILABLE_IOS(3_0);

2.在IOS9之后,苹果进一步完善了安全机制,必须在plist里面设置url scheme白名单,不然无法打开对应的应用

iOS APP内调起第三方地图_第1张图片

3.判断并装入数组

- (NSArray*)getInstalledMapAppWithEndLocation:(CLLocationCoordinate2D)endLocation

{

NSMutableArray*maps = [NSMutableArrayarray];

//苹果地图

NSMutableDictionary*iosMapDic = [NSMutableDictionarydictionary];

iosMapDic[@"title"] =@"苹果地图";

[mapsaddObject:iosMapDic];

//百度地图

if([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"baidumap://"]]) {

NSMutableDictionary*baiduMapDic = [NSMutableDictionarydictionary];

baiduMapDic[@"title"] =@"百度地图";

NSString*urlString = [[NSStringstringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=北京&mode=driving&coord_type=gcj02",endLocation.latitude,endLocation.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

baiduMapDic[@"url"] = urlString;

[mapsaddObject:baiduMapDic];

}

//高德地图

if([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"iosamap://"]]) {

NSMutableDictionary*gaodeMapDic = [NSMutableDictionarydictionary];

gaodeMapDic[@"title"] =@"高德地图";

NSString*urlString = [[NSStringstringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",@"导航功能",@"nav123456",endLocation.latitude,endLocation.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

gaodeMapDic[@"url"] = urlString;

[mapsaddObject:gaodeMapDic];

}

//谷歌地图

if([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"comgooglemaps://"]]) {

NSMutableDictionary*googleMapDic = [NSMutableDictionarydictionary];

googleMapDic[@"title"] =@"谷歌地图";

NSString*urlString = [[NSStringstringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"导航测试",@"nav123456",endLocation.latitude, endLocation.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

googleMapDic[@"url"] = urlString;

[mapsaddObject:googleMapDic];

}

//腾讯地图

if([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"qqmap://"]]) {

NSMutableDictionary*qqMapDic = [NSMutableDictionarydictionary];

qqMapDic[@"title"] =@"腾讯地图";

NSString*urlString = [[NSStringstringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%f,%f&to=终点&coord_type=1&policy=0",endLocation.latitude, endLocation.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

qqMapDic[@"url"] = urlString;

[mapsaddObject:qqMapDic];

}

returnmaps;

}

4.底部弹窗

UIActionSheet*myActionSheet = [[UIActionSheetalloc]init];

myActionSheet.delegate = self;

[myActionSheetaddButtonWithTitle:@"取消"];

[myActionSheetshowInView:[UIApplicationsharedApplication].keyWindow];

#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

 if(buttonIndex != -1) {

if(buttonIndex ==0) {

[selfnavAppleMap];

return;

}

NSDictionary*dic =self.maps[buttonIndex];

NSString*urlString = dic[@"url"];

[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:urlString]];

}

}

//苹果地图

- (void)navAppleMap

{

CLLocationCoordinate2D gps = [JZLocationConverterbd09ToWgs84:self.destinationCoordinate2D];

MKMapItem*currentLoc = [MKMapItemmapItemForCurrentLocation];

MKMapItem*toLocation = [[MKMapItemalloc]initWithPlacemark:[[MKPlacemarkalloc]initWithCoordinate:gpsaddressDictionary:nil]];

NSArray*items = @[currentLoc,toLocation];

NSDictionary*dic = @{

MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,

MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),

MKLaunchOptionsShowsTrafficKey : @(YES)

};

[MKMapItemopenMapsWithItems:itemslaunchOptions:dic];

}

你可能感兴趣的:(iOS APP内调起第三方地图)