您的位置:首页 > 移动开发 > IOS开发

ios开发中如何调用苹果自带地图导航

2017-08-09 11:41 399 查看
第一步:导入地图文件  #import <MapKit/MapKit.h>

  第二步:获取当前位置和目的地的经纬度,然后打开地图即可

  

  //获取当前位置

    MKMapItem *mylocation = [MKMapItem mapItemForCurrentLocation];

    

    //当前经维度

    float currentLatitude=mylocation.placemark.location.coordinate.latitude;

    float currentLongitude=mylocation.placemark.location.coordinate.longitude;

    

    CLLocationCoordinate2D coords1 = CLLocationCoordinate2DMake(currentLatitude,currentLongitude);

    

    //目的地位置

    coordinate.latitude=[[dataSource objectForKey:@"lat"] floatValue];

    coordinate.longitude=[[dataSource objectForKey:@"lng"] floatValue];

    

    

    CLLocationCoordinate2D coords2 = coordinate;

    

    // ios6以下,调用google map

    if (SYSTEM_VERSION_LESS_THAN(@"6.0"))

    {

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

        NSURL *aURL = [NSURL URLWithString:urlString];

        //打开网页google地图

        [[UIApplication sharedApplication] openURL:aURL];

    }

    else

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

    {

        //当前的位置

        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];

        //起点

        //MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords1 addressDictionary:nil]];

        //目的地的位置

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

        

        toLocation.name = @"目的地";

        NSString *myname=[dataSource objectForKey:@"name"];

        if (![XtomFunction xfunc_check_strEmpty:myname])

        {

            toLocation.name =myname;

        }

        

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

        NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES
};

        //打开苹果自身地图应用,并呈现特定的item

        [MKMapItem openMapsWithItems:items launchOptions:options];

    }

  

  通过这两步就可以轻松的开启苹果自带地图导航,感觉真是挺不错的,唯一的缺点是开启地图获取路线信息耗费的手机流量比较大,最好在wifi条件下调用。如果不是必须,尽量还是用高德或者百度自带的地图就好。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: