由于公司项目需要用到地图规划路线,之前公司是通过高德地图,自己设计,通过调用高德sdk中的方法去实现导航,规划路线,如驾车,步行,公交等,如今三方的地图应用已经把这个做的很好,其实我们没有必要再次开发,这样也可以节省开发时间,把时间花在项目中更需要时间的地方。
下面是通过openURL:拉起三方的地图应用
//传入目的地的地址和经纬度
- (void)GPSNavitationWithDestination:(CLLocationCoordinate2D)destination address:(NSString *)address{
//通过定位获取当前的经纬度
CLLocationCoordinate2D startCoor = _mapView.userLocation.location.coordinate;
CLLocationCoordinate2D endCoor = destination;
NSArray *startArray = [self exchangeLat:startCoor.latitude Lon:startCoor.longitude];
NSArray *endArray = [self exchangeLat:endCoor.latitude Lon:endCoor.longitude];
//拉起百度
if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
NSString *urlString1 = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:%@&mode=driving",startCoor.latitude, startCoor.longitude,[endArray[0] floatValue],[endArray[1] floatValue],address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString1]];
}else
//拉起高德
if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
//backScheme 设置为应用的scheme,可以返回到自身应用
NSString *urlString2 = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=房品汇&backScheme=applicationScheme&slat=%f&slon=%f&sname=当前位置&dlat=%f&dlon=%f&dname=%@&dev=0&m=0&t=0",startCoor.latitude,startCoor.longitude,endCoor.latitude, endCoor.longitude,address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString2]];
}else{
NSString *webStr = [[NSString stringWithFormat:@"http://api.map.baidu.com/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:%@&mode=driving®ion=我的位置&output=html&src=yourCompanyName|房品汇",[startArray[0] floatValue], [startArray[1] floatValue],[endArray[0] floatValue],[endArray[1] floatValue],address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:webStr]];
}
}
warning 应用在在拉起百度地图应用时和拉起web百度地图时会出现起始坐标和终点坐标偏移问题,所以在此要做一进行坐标转换,把火星坐标转化成百度坐标,
#pragma mark - 坐标转换
- (NSArray*)exchangeLat:(CGFloat)gg_lat Lon:(CGFloat)gg_lon {
CGFloat bd_lon;
CGFloat bd_lat;
const double x_pi = 3.14159265358979324 * 3000.f / 180.f;
double x = gg_lon, y = gg_lat;
double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
bd_lon = z * cos(theta) + 0.0065;
bd_lat = z * sin(theta) + 0.006;
NSArray *array = @[@(bd_lat),@(bd_lon)];
return array;
}
PS:拉起苹果地图
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc ]initWithCoordinate:endCoor addressDictionary:nil]];
toLocation.name = @"徐家汇";
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsMapCenterKey: [NSNumber numberWithBool:YES],MKLaunchOptionsMapTypeKey:[NSNumber numberWithInteger:0]}];
这种代码可复制性比较高,自己整理备份,方便后面使用!
轻松搞定地图导航,规划路线,有时候,这种借鉴就是一个字爽!
PS:来简书混,关注是必须的,点赞❤️ 是要给的!