iOS 跳转到地图后导航(高德地图,百度地图,腾讯地图,苹果手机原生的地图)

1.现在info.plist里面如下图所示添加


2.在下图输入框中输入地名,然后点击前往目的地会出现如下图所示。(如果你不知道地方名如何转化为经纬度请看我的另一篇博客:http://blog.csdn.net/chenyongkai1/article/details/51891135)

iOS 跳转到地图后导航(高德地图,百度地图,腾讯地图,苹果手机原生的地图)_第1张图片

3.添加下面的代理

UIActionSheetDelegate,CLLocationManagerDelegate


4.下面是最核心代码

先调用下面的方法

- (void)showMapNavigationViewWithtargetLatitude:(double)targetLatitude targetLongitute:(double)targetLongitute toName:(NSString *)name;



4.调用完最核心的代码后下面的所有的主干方法都在在上面的方法执行后都会运行一次。

(NSArray *)checkHasOwnApp{

    NSArray *mapSchemeArr =@[@"iosamap://navi",@"baidumap://map/",@"qqmap://"];

    

    NSMutableArray *appListArr = [[NSMutableArrayalloc] initWithObjects:@"苹果地图",nil];

    

    for (int i =0; i < [mapSchemeArr count]; i++) {

        if ([[UIApplicationsharedApplication] canOpenURL:[NSURLURLWithString:[NSStringstringWithFormat:@"%@",[mapSchemeArrobjectAtIndex:i]]]]) {

            if (i ==0) {

                [appListArr addObject:@"高德地图"];

            }elseif (i == 1){

                [appListArr addObject:@"百度地图"];

            }elseif (i == 2){

                [appListArr addObject:@"腾讯地图"];

            }elseif (i == 3){

                

            }

        }

    }

    

    return appListArr;

}

- (void)showMapNavigationViewFormcurrentLatitude:(double)currentLatitude currentLongitute:(double)currentLongitute TotargetLatitude:(double)targetLatitude targetLongitute:(double)targetLongitute toName:(NSString *)name{

    _currentLatitude = currentLatitude;

    _currentLongitute = currentLongitute;

    _targetLatitude = targetLatitude;

    _targetLongitute = targetLongitute;

    _name = name;

    NSArray *appListArr = [JXMapNavigationViewcheckHasOwnApp];

    NSString *sheetTitle = [NSStringstringWithFormat:@"导航到 %@",name];

    UIActionSheet *sheet;

    if ([appListArrcount] == 1) {

        sheet = [[UIActionSheetalloc] initWithTitle:sheetTitledelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:appListArr[0],nil];

    }elseif ([appListArr count] ==2) {

        sheet = [[UIActionSheetalloc] initWithTitle:sheetTitledelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:appListArr[0],appListArr[1],nil];

    }elseif ([appListArr count] ==3){

        sheet = [[UIActionSheetalloc] initWithTitle:sheetTitledelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:appListArr[0],appListArr[1],appListArr[2],nil];

    }elseif ([appListArr count] ==4){

        sheet = [[UIActionSheetalloc] initWithTitle:sheetTitledelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:appListArr[0],appListArr[1],appListArr[2],appListArr[3],nil];

    }elseif ([appListArr count] ==5){

        sheet = [[UIActionSheetalloc] initWithTitle:sheetTitledelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:appListArr[0],appListArr[1],appListArr[2],appListArr[3],appListArr[4],nil];

    }

    sheet.actionSheetStyle =UIActionSheetStyleBlackOpaque;

    [sheet showInView:self];

    

}



#pragma mark-UIActionSheetDelegate

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

    

    NSString *name =_name;

    

    float ios_version=[[[UIDevicecurrentDevice] systemVersion]floatValue];

    NSString *btnTitle = [actionSheetbuttonTitleAtIndex:buttonIndex];

    

    

    if (buttonIndex ==0) {

        if (ios_version <6.0) {//ios6调用goole网页地图

            NSString *urlString = [[NSStringalloc]

                                  initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",

                                  _currentLatitude,_currentLongitute,_targetLatitude,_targetLongitute];

            

            NSURL *aURL = [NSURLURLWithString:urlString];

            //打开网页google地图

            [[UIApplicationsharedApplication] openURL:aURL];

        }else{//起点

            CLLocationCoordinate2D from =CLLocationCoordinate2DMake(_currentLatitude,_currentLongitute);

            MKMapItem *currentLocation = [[MKMapItemalloc] initWithPlacemark:[[MKPlacemarkalloc] initWithCoordinate:fromaddressDictionary:nil]];

            currentLocation.name =@"我的位置";

            

            //终点

            CLLocationCoordinate2D to =CLLocationCoordinate2DMake(_targetLatitude,_targetLongitute);

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

            NSLog(@"网页google地图:%f,%f",to.latitude,to.longitude);

            toLocation.name = name;

            NSArray *items = [NSArrayarrayWithObjects:currentLocation, toLocation,nil];

            NSDictionary *options =@{

                                     MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,

                                     MKLaunchOptionsMapTypeKey:

                                          [NSNumbernumberWithInteger:MKMapTypeStandard],

                                     MKLaunchOptionsShowsTrafficKey:@YES

                                     };

            

            //打开苹果自身地图应用

            [MKMapItemopenMapsWithItems:items launchOptions:options];

        }

    }

    if ([btnTitleisEqualToString:@"谷歌地图"]) {

        NSString *urlStr = [NSStringstringWithFormat:@"comgooglemaps://?saddr=%.8f,%.8f&daddr=%.8f,%.8f&directionsmode=transit",_currentLatitude,_currentLongitute,_targetLatitude,_targetLongitute];

        [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlStr]];

    }

    elseif ([btnTitle isEqualToString:@"高德地图"]){

        NSString *urlString = [[NSStringstringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=%f&slon=%f&sname=%@&did=BGVIS2&dlat=%f&dlon=%f&dname=%@&dev=0&m=0&t=0",_currentLatitude,_currentLongitute,@"我的位置",_targetLatitude,_targetLongitute,_name]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        NSURL *r = [NSURLURLWithString:urlString];

        [[UIApplicationsharedApplication] openURL:r];

        //        NSLog(@"%@",_lastAddress);

        

    }

    

    elseif ([btnTitle isEqualToString:@"腾讯地图"]){

        

        NSString *urlStr = [NSStringstringWithFormat:@"qqmap://map/routeplan?type=drive&fromcoord=%f,%f&tocoord=%f,%f&policy=1",_currentLatitude,_currentLongitute,_targetLatitude,_targetLongitute];

        NSURL *r = [NSURLURLWithString:urlStr];

        [[UIApplicationsharedApplication] openURL:r];

    }

    elseif([btnTitle isEqualToString:@"百度地图"])

    {

        double AdressLat,AdressLon;

        double NowLat,NowLon;

        

        bd_encrypt(_targetLatitude,_targetLongitute, &AdressLat, &AdressLon);

        bd_encrypt(_currentLatitude,_currentLongitute, &NowLat, &NowLon);

        NSString *stringURL = [NSStringstringWithFormat:@"baidumap://map/direction?origin=%f,%f&destination=%f,%f&&mode=driving",NowLat,NowLon,AdressLat,AdressLon];

        NSURL *url = [NSURLURLWithString:stringURL];

        [[UIApplicationsharedApplication] openURL:url];

    }elseif (actionSheet.cancelButtonIndex==buttonIndex){

   //解决点击取消后重复出现选择框的问题

        [actionSheet removeFromSuperview];

        [selfstopLocation];

    }

    

    

    

}

- (void)showMapNavigationViewWithtargetLatitude:(double)targetLatitude targetLongitute:(double)targetLongitute toName:(NSString *)name{

    self.newcllocation=[[CLLocationalloc]init];

    [selfstartLocation];

    _targetLatitude = targetLatitude;

    _targetLongitute = targetLongitute;

    _name = name;

     [selfshowMapNavigationViewFormcurrentLatitude:self.newcllocation.coordinate.latitudecurrentLongitute:self.newcllocation.coordinate.longitudeTotargetLatitude:_targetLatitudetargetLongitute:_targetLongitutetoName:_name];

}

//获取经纬度

-(void)startLocation

{

    if([CLLocationManagerlocationServicesEnabled] && [CLLocationManagerauthorizationStatus] != kCLAuthorizationStatusDenied)

    {

        _manager=[[CLLocationManageralloc]init];

        _manager.delegate=self;

        _manager.desiredAccuracy =kCLLocationAccuracyBest;

        [_managerrequestAlwaysAuthorization];

        _manager.distanceFilter=100;

        [_managerstartUpdatingLocation];

    }

    else

    {

        UIAlertView *alvertView=[[UIAlertViewalloc]initWithTitle:@"提示"message:@"请到设置->隐私,打开定位服务"delegate:nilcancelButtonTitle:@"确定"otherButtonTitles: nil];

        [alvertView show];

    }

    

}

#pragma mark CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

{

    self.newcllocation=newLocation;

   

    

}

- (void)locationManager:(CLLocationManager *)manager

       didFailWithError:(NSError *)error{

    [selfstopLocation];

    

}

-(void)stopLocation

{

    _manager =nil;

}




大家有什么问题可以关注我交流或者给我留言。



你可能感兴趣的:(地图)