美文网首页
ios--调用苹果自带/高德/百度地图进行导航

ios--调用苹果自带/高德/百度地图进行导航

作者: 肉肉要次肉 | 来源:发表于2020-03-26 16:35 被阅读0次

项目中用到点击店家地址跳到地图导航,app本身没有其他地图相关功能,所以没有必要去集成那些sdk,文件大还很麻烦。直接调用多简单方便,还不用占用自己app。最可贵的是苹果还有自带地图呢

在调用地图之前,先啰嗦一下,由于项目中只返回了店家地址,没有经纬度,所以这里,我要先记录一下,用苹果自带方法,将地址转换成经纬度

1、引入头文件 #import<CoreLocation/CoreLocation.h>

2、声明属性

{

    CLGeocoder*_geocoder;

}

@property(nonatomic,assign)CLLocationCoordinate2D  coordinate;

3、在你需要调用的地方

_geocoder= [[CLGeocoderalloc]init];

[self getCoordinateByAddress:_address];

#pragma mark根据地名确定地理坐标

-(void)getCoordinateByAddress:(NSString *)address{

//地理编码

  [_geocodergeocodeAddressString:addresscompletionHandler:^(NSArray*placemarks,NSError*error) {

//取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址

     CLPlacemark*placemark=[placemarksfirstObject];

     CLLocation*location=placemark.location;//位置

     CLRegion*region=placemark.region;//区域

     NSDictionary*addressDic= placemark.addressDictionary;//详细地址信息字典,包含以下部分信息

//        NSString *name=placemark.name;//地名

//        NSString *thoroughfare=placemark.thoroughfare;//街道

//        NSString *subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等

//        NSString *locality=placemark.locality; // 城市

//        NSString *subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑

//        NSString *administrativeArea=placemark.administrativeArea; // 州

//        NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息

//        NSString *postalCode=placemark.postalCode; //邮编

//        NSString *ISOcountryCode=placemark.ISOcountryCode; //国家编码

//        NSString *country=placemark.country; //国家

//        NSString *inlandWater=placemark.inlandWater; //水源、湖泊

//        NSString *ocean=placemark.ocean; // 海洋

//        NSArray *areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标

     NSLog(@"位置:%@,区域:%@,详细信息:%@",location,region,addressDic);

     CLLocationDegreeslatitude = location.coordinate.latitude;

     CLLocationDegreeslongitude = location.coordinate.longitude;

     self.coordinate = CLLocationCoordinate2DMake(latitude,longitude);

 NSLog(@"纬度-->%lf,经度-->%lf",latitude,longitude);

   //传给接口的纬度和经度

//      self->_latitude = [NSString stringWithFormat:@"%lf",latitude];

//      self->_longitude = [NSString stringWithFormat:@"%lf",longitude];

   }];

}

以上就是先把地址转换成经纬度的过程。

接下来,就是将经纬度传给苹果,高德,百度地图进行跳转导航的过程啦~

1、调用苹果自带地图,记得引用头文件#import<MapKit/MapKit.h>

2、Info.plist文件中加入白名单

高德:iosamap

百度:baidumap

3、代码展示

#pragma mark-- 根据经纬度跳地图导航

-(void)tapAddressAction{

    __weaktypeof(self) weakSelf =self;

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"选择地图" preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction*action1 = [UIAlertActionactionWithTitle:@"苹果自带地图"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

        MKMapItem*currentLocation = [MKMapItemmapItemForCurrentLocation];

        MKMapItem *tolocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:weakSelf.coordinate addressDictionary:nil]];

        tolocation.name= weakSelf.address;

        [MKMapItem openMapsWithItems:@[currentLocation,tolocation]launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,

        MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];

    }];

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {

        UIAlertAction*action2 = [UIAlertActionactionWithTitle:@"高德地图"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

            NSString *urlsting =[[NSString stringWithFormat:@"iosamap://navi?sourceApplication= &backScheme= &lat=%f&lon=%f&dev=0&style=2",weakSelf.coordinate.latitude,weakSelf.coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting]];

        }];

        [alertaddAction:action2];

    }

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {

        UIAlertAction*action3 = [UIAlertActionactionWithTitle:@"百度地图"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

            NSString *urlsting =[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",weakSelf.coordinate.latitude,weakSelf.coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting]];

        }];

        [alertaddAction:action3];

    }

    UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }];

    [alertaddAction:action1];

    [alertaddAction:action4];

    [self.navigationController presentViewController:alert animated:YES completion:nil];

}

我这里会先判断是否安装了高德和百度地图,如果安装了,会在选择弹窗中显示,没安装就不显示,只显示一个苹果自带地图

相关文章

网友评论

      本文标题:ios--调用苹果自带/高德/百度地图进行导航

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