在项目中用到了判断用户安装了哪种地图,直接根据经纬度跳转到地图软件中,并且绘制路线,非常实用
------ 判断用户安装了哪种地图 ------
/**
* 判断用户安装了哪种地图
* @author sweet
* @param parameter
* @return result
*/
- (void)checkMap
{
NSMutableArray *mapArray = [NSMutableArray new];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]){
NSLog(@"外部安装了谷歌地图");
[mapArray addObject:@"Google地图"];
}
if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
NSLog(@"外部安装了高德地图");
[mapArray addObject:@"高德地图"];
}
if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
NSLog(@"外部安装了百度地图");
[mapArray addObject:@"百度地图"];
}
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]){
NSLog(@"外部安装了腾讯地图");
[mapArray addObject:@"腾讯地图"];
}
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"sgmap://"]]){
NSLog(@"外部安装了搜狗地图");
[mapArray addObject:@"搜狗地图"];
}
UIActionSheet *mapSheet = [[UIActionSheet alloc] initWithTitle:@"选择地图" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"苹果地图" otherButtonTitles:nil, nil];
for (int i = 0; i < mapArray.count; i++) {
[mapSheet addButtonWithTitle:mapArray[I]];
}
NSLog(@"mapArray===%@",mapArray);
[mapSheet addButtonWithTitle:@"取消"];
mapSheet.cancelButtonIndex = mapSheet.numberOfButtons - 1;
[mapSheet showInView:self.view];
}
------ 跳转到对应的地图软件 ------
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *str = [actionSheet buttonTitleAtIndex:buttonIndex];
//---起始点坐标
NSString *startLatitude = @"34.1800850000";
NSString *startLongitude = @"108.5706980000";
//---终点坐标
NSString *endLatitude = @"34.1900850000";
NSString *endLongitude = @"108.8706980000";
CLLocationCoordinate2D destinationCoordinate = CLLocationCoordinate2DMake([endLatitude floatValue],[endLongitude floatValue]);
NSString *shopName = @"噢噢噢噢";
NSLog(@"str =%@",str);
if ([str isEqualToString:@"苹果地图"]) {
NSLog(@"自带地图导航");
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:destinationCoordinate addressDictionary:nil]];
toLocation.name = @"哈哈";
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}
if ([str isEqualToString:@"高德地图"]) {
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
NSString *urlString=[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&backScheme=applicationScheme&slat=%f&slon=%f&sname=我&dlat=%f&dlon=%f&dname=%@&dev=0&m=0&t=1",[startLatitude floatValue],[startLongitude floatValue],destinationCoordinate.latitude,destinationCoordinate.longitude,shopName];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *aURL = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:aURL];
}
}
if ([str isEqualToString:@"百度地图"]){
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){
NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:%@&mode=transit",
[startLatitude floatValue],[startLongitude floatValue],destinationCoordinate.latitude,destinationCoordinate.longitude,shopName];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *aURL = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:aURL];
}
}
if ([str isEqualToString:@"Google地图"]) {
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?saddr=&daddr=%f,%f¢er=%f,%f&directionsmode=transit",[startLatitude floatValue],[startLongitude floatValue],destinationCoordinate.latitude,destinationCoordinate.longitude];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *aURL = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:aURL];
}
}
if ([str isEqualToString:@"腾讯地图"]) {
NSString *urlStr = [NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&fromcoord=%f,%f&tocoord=%f,%f&policy=1",[startLatitude floatValue],[startLongitude floatValue],destinationCoordinate.latitude,destinationCoordinate.longitude];
NSURL *r = [NSURL URLWithString:urlStr];
[[UIApplication sharedApplication] openURL:r];
}
if ([str isEqualToString:@"搜狗地图"]) {
NSString *urlStr = [NSString stringWithFormat:@"sgmap://map/routeplan?type=drive&fromcoord=%f,%f&tocoord=%f,%f&policy=1",[startLatitude floatValue],[startLongitude floatValue],destinationCoordinate.latitude,destinationCoordinate.longitude];
NSURL *r = [NSURL URLWithString:urlStr];
[[UIApplication sharedApplication] openURL:r];
}
}
------ 注意 ------
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]
还有iOS9 是需要在plist文件配置白名单的LSApplicationQueriesSchemes