美文网首页RNiOS 开发继续加油
IOS 在自己的app内唤起地图APP

IOS 在自己的app内唤起地图APP

作者: Mr_Jee | 来源:发表于2016-11-15 11:01 被阅读363次

    选择背景:由于地图的SDK一般较大,在app内部实现线路规划、导航等功能比较麻烦,而且耗时,所以就选择在app内部唤起地图app(如高德地图、百度地图、腾讯地图、苹果地图等)以实现同样的功能。

    效果见下图:

    ![IMG_0330.PNG](http:https://img.haomeiwen.com/i2972094/70e1580907587d39.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

    代码如下:(具体会根据手机上安装的地图app来显示)

    #pragma mark - 选择导航app
    - (void)actionBtnClick {
        //当前的位置
        CLLocationCoordinate2D currentLoc = self.mapView.userLocation.location.coordinate;
        MKPlacemark *curPlacemark = [[MKPlacemark alloc] initWithCoordinate:currentLoc addressDictionary:nil];
        MKMapItem *curLocationItem = [[MKMapItem alloc] initWithPlacemark:curPlacemark];
        curLocationItem.name = @"当前位置";
        //目的地的位置
        CLLocationCoordinate2D toLocation;
        toLocation.longitude = _longi.floatValue;
        toLocation.latitude = _lati.floatValue;
        MKPlacemark *toPlacemark = [[MKPlacemark alloc] initWithCoordinate:toLocation addressDictionary:nil];
        MKMapItem *toLocationItem = [[MKMapItem alloc] initWithPlacemark:toPlacemark];
        toLocationItem.name = _shopName;
        //选择要唤起的app
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
            UIAlertAction *gaode = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=ApplicationName&sid=BGVIS1&slat=%f&slon=%f&sname=当前位置&did=BGVIS2&dlat=%f&dlon=%f&dname=%@&dev=0&m=0&t=0", currentLoc.latitude, currentLoc.longitude, _lati.floatValue, _longi.floatValue, _shopName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                [CGOpenUrlTool openUrl:[NSURL URLWithString:urlString]];
            }];
            [alert addAction:gaode];
        }
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
            UIAlertAction *baidu = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=%f,%f&destination=latlng:%f,%f|name=%@&mode=driving&coord_type=gcj02",currentLoc.latitude, currentLoc.longitude, _lati.floatValue, _longi.floatValue, _shopName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                [CGOpenUrlTool openUrl:[NSURL URLWithString:urlString]];
            }];
            [alert addAction:baidu];
        }
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
            UIAlertAction *tencent = [UIAlertAction actionWithTitle:@"腾讯地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=当前位置&fromcoord=%f,%f&type=drive&tocoord=%f,%f&to=%@&coord_type=1&policy=0", currentLoc.latitude, currentLoc.longitude, _lati.floatValue, _longi.floatValue, _shopName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                [CGOpenUrlTool openUrl:[NSURL URLWithString:urlString]];
            }];
            [alert addAction:tencent];
        }
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]]) {
            UIAlertAction *apple = [UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                NSArray *items = @[curLocationItem,toLocationItem];
                NSDictionary *options = @{
                                      MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDefault,
                                      MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
                                      MKLaunchOptionsShowsTrafficKey : @(YES)
                                      };
                
                //打开苹果自身地图应用,并呈现特定的item
                [MKMapItem openMapsWithItems:items launchOptions:options];
            }];
            [alert addAction:apple];
        }
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:nil];
    }
    

    相关文章

      网友评论

        本文标题:IOS 在自己的app内唤起地图APP

        本文链接:https://www.haomeiwen.com/subject/pkikpttx.html