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

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

输入目的地地址时最好填入区, 不然有时会定位不到

http://developer.baidu.com/map/wiki/index.php?title=uri/api/ios

这个网址是百度地图官方的, 提供web和ios跳转到百度地图app的各种url, 诸如可以输入起点终点进行导航, 或者把输入的地址显示在百度地图上等等

下面这个对地址反编译成经纬度就已经要用到了, 更别说后面的定位了

#import

要调用自带的高德地图, 就要

#import

我的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 * _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];

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值