iOS 调起第三方应用,进行定位,导航......

我们常用的地图应用有高德地图、百度地图、谷歌地图(好像国内不怎用...0),当然我们iOS有系统自带的地图,哈哈,多方便啊,直接拿过来用就是咯,下面我们讲讲怎么拉起第三方地图应用来进行定位导航等。。。

  • 拉起第三方应用,必不可少的是URLSchemes
系统地图:导入#import 就好
高德地图:iosamap://
百度地图:baidumap://
//我们要在info.plist配置白名单
LSApplicationQueriesSchemes
    
        iosamap
        baidumap
    
  • 不多说,直接上代码
 //百度地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=%@&mode=driving&coord_type=gcj02",self.shopDetailModel.address]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     //打开百度地图
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }
    
    //高德地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
     //百度坐标转换火星坐标 (由于我们公司的后台是百度地图的服务,所以在这里我做了个坐标转化)
        const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        NSString *lo = [NSString stringWithFormat:@"%f",endLocation.longitude];
         NSString *la = [NSString stringWithFormat:@"%f",endLocation.latitude];
        double x = [lo doubleValue] - 0.0065, y = [la doubleValue] - 0.006;
        double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
        double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
        double  gg_lon = z * cos(theta);
        double  gg_lat = z * sin(theta);
        
        NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=Zhongyi&sid=BGVIS1&slat=%lf&slon=%lf&sname=%@&did=BGVIS2&dlat=%lf&dlon=%lf&dname=%@&dev=0&m=0&t=0",[self.latitude doubleValue],[self.longitude doubleValue],@"我的位置",gg_lat,gg_lon,self.shopDetailModel.address]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     //打开高德地图
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

     //系统地图
        const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
                    //百度坐标转换火星坐标
         NSString *lo = [NSString stringWithFormat:@"%f",self.shopDetailModel.longitude.doubleValue];
                    NSString *la = [NSString stringWithFormat:@"%f",self.shopDetailModel.latitude.doubleValue];
                    double x = [lo doubleValue] - 0.0065, y = [la doubleValue] - 0.006;
                    double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
                    double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
                    double  gg_lon = z * cos(theta);
                    double  gg_lat = z * sin(theta);
                    CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(gg_lat, gg_lon);
                    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
                    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:loc addressDictionary:nil]];
                    toLocation.name = self.shopDetailModel.address;
                    [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
                                   launchOptions:@{
MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard],MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];

                }
            }];


  • 关于高德地图url拼接的详细说明可以参考附上传送门http://lbs.amap.com/api/amap-mobile/guide/ios/ios-uri-information 而百度是http://lbsyun.baidu.com/index.php?title=uri/api/ios

你可能感兴趣的:(iOS 调起第三方应用,进行定位,导航......)