提供多个地图选择

来源:互联网 发布:网络连接图标是灰色的 编辑:程序博客网 时间:2024/05/22 03:45

#import <MapKit/MapKit.h>

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)//用来获取手机的系统,判断系统是多少

设置  

UIActionSheetDelegate 代理

@property(strong ,nonatomic)NSMutableArray *availableMaps;   定义可变数组

 

定义好 地点和终点 (导航的必要条件)

    

        CLLocationCoordinate2D startCoor;

    CLLocationCoordinate2D endCoor;

    


 startCoor =CLLocationCoordinate2DMake([YFMapMessagesharedInstance].coordinate.latitude,[YFMapMessagesharedInstance].coordinate.longitude);

    

    if (_dataArray.count==1) {

        YFMapItemModel *model =_dataArray[0];

       

        endCoor =CLLocationCoordinate2DMake(model.merchantsCoord.latitude, model.merchantsCoord.longitude);

    }


    self.availableMaps=[[NSMutableArrayalloc] init];

    

//    if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { // ios6以下,调用google map

//        

//        NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude];

//        //        @"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude

//        urlString =  [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

//        NSURL *aURL = [NSURL URLWithString:urlString];

//        [[UIApplication sharedApplication] openURL:aURL];

//    } else { // 直接调用ios自己带的apple map

//        

//        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];

//        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];

//        toLocation.name = @"to name";

//        

//        [MKMapItem openMapsWithItems:@[currentLocation, toLocation]

//                       launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];

//        

//    }

    

    UIButton *btn=[UIButtonbuttonWithType:UIButtonTypeCustom];

    [btn setTitle:@"开始导航"forState:UIControlStateNormal];

    btn.titleLabel.font=FONT(19);

    [btn setTitleColor:[UIColorredColor] forState:UIControlStateNormal];

    [btn addTarget:selfaction:@selector(pressMore)forControlEvents:UIControlEventTouchUpInside];

    [btn setFrame:CGRectMake(kScreenWidth-100,kScreenHeight-120,80, 40)];

    [self.viewaddSubview:btn];




-(void)pressMore

{

    

#if 0

    //注释掉通过百度SDK 的导航

    if (![self checkServicesInited])

        return;

    _naviType = BN_NaviTypeReal;

    [self startNavi];

#else

    [selfavailableMapsApps];

    UIActionSheet *action = [[UIActionSheetalloc] init];

    [action setTitle:@"请选择地图" ];

    [action addButtonWithTitle:@"使用高德地图导航"];

    for (NSDictionary *dicin self.availableMaps) {

        [action addButtonWithTitle:[NSStringstringWithFormat:@"使用%@导航", dic[@"name"]]];

    }

    [action addButtonWithTitle:@"取消"];

    action.cancelButtonIndex =self.availableMaps.count +1;

    action.delegate =self;

    [action showInView:self.view];

    

    

    

#endif


}

- (void)availableMapsApps {

    [self.availableMapsremoveAllObjects];

    

//    CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;

//    CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);

    NSString *toName =@"to name";

    

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

        NSString *urlString = [NSStringstringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:%@&mode=transit",

                               startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude, toName];

        

        NSDictionary *dic =@{@"name":@"百度地图",

                              @"url": urlString};

        [self.availableMapsaddObject:dic];

    }

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

        NSString *urlString = [NSStringstringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=applicationScheme&poiname=fangheng&poiid=BGVIS&lat=%f&lon=%f&dev=0&style=3",

                               @"云华时代",endCoor.latitude,endCoor.longitude];

        

        NSDictionary *dic =@{@"name":@"高德地图",

                              @"url": urlString};

        [self.availableMapsaddObject:dic];

    }

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

        NSString *urlString = [NSStringstringWithFormat:@"comgooglemaps://?saddr=&daddr=%f,%f¢er=%f,%f&directionsmode=transit",endCoor.latitude,endCoor.longitude,startCoor.latitude,startCoor.longitude];

        

        NSDictionary *dic =@{@"name":@"Google Maps",

                              @"url": urlString};

        [self.availableMapsaddObject:dic];

    }

}


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

    if (buttonIndex ==0) {

//        CLLocationCoordinate2D startCoor =

//        CLLocationCoordinate2D endCoor =

        if (SYSTEM_VERSION_LESS_THAN(@"6.0")) {// ios6以下,调用google map

            

            NSString *urlString = [[NSStringalloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude];

            //        @"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude

            urlString =  [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            NSURL *aURL = [NSURLURLWithString:urlString];

            [[UIApplicationsharedApplication] openURL:aURL];

        } else{//直接调用ios自己带的apple map

            

            MKMapItem *currentLocation = [MKMapItemmapItemForCurrentLocation];

            MKPlacemark *placemark = [[MKPlacemarkalloc] initWithCoordinate:endCooraddressDictionary:nil];

            MKMapItem *toLocation = [[MKMapItemalloc] initWithPlacemark:placemark];

            toLocation.name =@"to name";

            

            [MKMapItemopenMapsWithItems:@[currentLocation, toLocation]

                           launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumbernumberWithBool:YES]}];

            

        }

    }elseif (buttonIndex < self.availableMaps.count+1) {

        NSDictionary *mapDic =self.availableMaps[buttonIndex-1];

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

        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        NSURL *url = [NSURLURLWithString:urlString];

        DLog(@"\n%@\n%@\n%@", mapDic[@"name"], mapDic[@"url"], urlString);

        [[UIApplicationsharedApplication] openURL:url];

    }

}


刚开始不能跳转 ,因为要在plist里面设置 跳转到app的 标示


在更新iOS9后原来的方法不起做用了。解决办法:

info.plist添加白名

<key>LSApplicationQueriesSchemes</key>

<array>

     <string>baidumap</string>

     <string>iosamap</string>

<string>comgooglemaps<string>

</array>


1 0
原创粉丝点击