由于公司項(xiàng)目需要用到地圖規(guī)劃路線,之前公司是通過高德地圖,自己設(shè)計(jì)裸诽,通過調(diào)用高德sdk中的方法去實(shí)現(xiàn)導(dǎo)航,規(guī)劃路線嘱函,如駕車埂蕊,步行,公交等函似,如今三方的地圖應(yīng)用已經(jīng)把這個(gè)做的很好喉童,其實(shí)我們沒有必要再次開發(fā),這樣也可以節(jié)省開發(fā)時(shí)間重抖,把時(shí)間花在項(xiàng)目中更需要時(shí)間的地方祖灰。
下面是通過openURL:拉起三方的地圖應(yīng)用
//傳入目的地的地址和經(jīng)緯度
- (void)GPSNavitationWithDestination:(CLLocationCoordinate2D)destination address:(NSString *)address{
//通過定位獲取當(dāng)前的經(jīng)緯度
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 設(shè)置為應(yīng)用的scheme恨统,可以返回到自身應(yīng)用
NSString *urlString2 = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=房品匯&backScheme=applicationScheme&slat=%f&slon=%f&sname=當(dāng)前位置&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 應(yīng)用在在拉起百度地圖應(yīng)用時(shí)和拉起web百度地圖時(shí)會(huì)出現(xiàn)起始坐標(biāo)和終點(diǎn)坐標(biāo)偏移問題畜埋,所以在此要做一進(jìn)行坐標(biāo)轉(zhuǎn)換畴蒲,把火星坐標(biāo)轉(zhuǎn)化成百度坐標(biāo)模燥,
#pragma mark - 坐標(biāo)轉(zhuǎn)換
- (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]}];
這種代碼可復(fù)制性比較高么翰,自己整理備份,方便后面使用浩嫌!
輕松搞定地圖導(dǎo)航,規(guī)劃路線追迟,有時(shí)候伐坏,這種借鑒就是一個(gè)字爽桦沉!
PS:來(lái)簡(jiǎn)書混,關(guān)注是必須的纯露,點(diǎn)贊?? 是要給的!