根据输入的地址跳转到百度地图或者高德地图进行路线导航

写在前面: 尽管精确度已然调至最佳, 但还是会有一定程度的定位误差, 作为开发者我已然尽力, 只能希望apple官方做些优化吧

输入目的地地址时最好填入区, 不然有时会定位不到
http://developer.baidu.com/map/wiki/index.php?title=uri/api/ios
这个网址是百度地图官方的, 提供web和ios跳转到百度地图app的各种url, 诸如可以输入起点终点进行导航, 或者把输入的地址显示在百度地图上等等

下面这个对地址反编译成经纬度就已经要用到了, 更别说后面的定位了
#import <CoreLocation/CoreLocation.h>

要调用自带的高德地图, 就要
#import <MapKit/MapKit.h>

我的demo控件只是创建了一个开始跳转到地图的按钮和输入目的地的textField而已

以下是属性

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *buttonOfBeginLocate;

//目的地经纬度
@property (nonatomic, assign) CGFloat longitude;
@property (nonatomic, assign) CGFloat latitude;

//目前所在地经纬度
@property (nonatomic, assign) CGFloat currentLatitude;
@property (nonatomic, assign) CGFloat currentLongitude;

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLGeocoder *geocoder;

写俩懒加载

 #pragma mark - 懒加载
- (CLLocationManager *)locationManager{

    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        // 设置定位精确度到米
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // 设置过滤器为无
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        // 一个是requestAlwaysAuthorization,一个是requestWhenInUseAuthorization
        [_locationManager requestWhenInUseAuthorization];//这句话ios8以上版本使用。

    }
    return _locationManager;
}

- (CLGeocoder *)geocoder{

    if (!_geocoder) {
        _geocoder = [[CLGeocoder alloc] init];
    }
    return _geocoder;
}

在button点击事件里

  - (void)beginLocate:(UIButton *)button{

   [self.locationManager startUpdatingLocation];

   [self translateAddress];

   //设备安装了百度地图
  if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){
      [self dumpToBaidu];
  }
  //没安装百度地图, 跳转到自带的高德
  else{
      [self testAppleMapWithLatitude:_latitude longitude:_longitude];
  }
}


#pragma mark - 定位协议方法
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

    _currentLatitude = newLocation.coordinate.latitude;
    _currentLongitude = newLocation.coordinate.longitude;
    if (_currentLatitude && _currentLongitude) {
        [manager stopUpdatingLocation];
    }

}


#pragma mark - 目的地地址编译为经纬度. 
//地址尽量有区, 比如龙湖区
- (void)translateAddress{

    [self.geocoder geocodeAddressString:_textField.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    
        if (placemarks.count > 0 && error == nil) {
        
            CLPlacemark *placemark = placemarks.firstObject;
        
            _longitude = placemark.location.coordinate.longitude;
            _latitude = placemark.location.coordinate.latitude;
        
        }
        else if (placemarks.count == 0 && error == nil){
            NSLog(@"placemarks元素为0");
        }else if(error != nil){
            NSLog(@"an arror occurred = %@", error);
        }
    }];

}

#pragma mark - 跳转到百度地图
- (void)dumpToBaidu{

    //转成UTF8  [NSCharacterSet URLQueryAllowedCharacterSet]
    //四个参数分别是, 当前位置纬度, 经度, 目的地纬度, 经度
    NSString *url4 = [[NSString stringWithFormat:@"baidumap://map/direction?origin=%f,%f&destination=%f,%f&mode=driving&src=webapp.navi.yourCompanyName.yourAppName", _currentLatitude, _currentLongitude, _latitude, _longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

    //根据起点终点跳转到百度地图并进行驾车导航
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url4]];

}

#pragma mark - 跳转到苹果高德地图
-(void)testAppleMapWithLatitude:(CGFloat)latitude longitude:(CGFloat)longitude{

    //    CLLocationCoordinate2D coords1 = CLLocationCoordinate2DMake(30.691793,104.088264);

    //    CLLocationCoordinate2D coords2 = CLLocationCoordinate2DMake(30.691293,104.088264);


    CLLocationCoordinate2D coords1 = CLLocationCoordinate2DMake(_currentLatitude, _currentLongitude);

//    CLLocationCoordinate2D coords2 = CLLocationCoordinate2DMake(40.001,116.404);

    CLLocationCoordinate2D coords2 = CLLocationCoordinate2DMake(latitude, longitude);

    //这个判断我没试过, 现在也没几个用ios6了吧
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0){
    
        // ios6以下,调用google map {
    
        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]];
    currentLocation.name = @"目前位置";
    
    
        //目的地的位置
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords2 addressDictionary:nil]];
    
        //显示在地图上的目的地名称
        toLocation.name = @"目的地";
    
        NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation, nil];
    
        NSDictionary *options = @{
                              MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES
                              }; //打开苹果自身地图应用,并呈现特定的item
    
        [MKMapItem openMapsWithItems:items launchOptions:options];
    
    }

}

最后感谢 http://blog.csdn.net/hengshujiyi/article/details/45560609 给我灵感

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,637评论 25 708
  • 昨晚夜班,见到宝贝已经九点多了,在外婆家,昨晚表现不是很好,把床尿湿了,和外婆接触比较少点,宝贝应该是有点不适应的...
    亲然阅读 255评论 0 0
  • 我是家里的老小,我从小是家里被照顾的那个,用伯父他们的话说,但我从来没有觉得自己是被照顾的,我只是不需要去干农...
    王翠英阅读 260评论 0 0
  • CGAffineTransform是一个映射转换3*3的矩阵,用来绘画2D图像。可以实现放大、缩小、平移。先看看其...
    yuandiLiao阅读 9,304评论 0 10