最近在做導(dǎo)航混驰,所以把自己找到的資料總結(jié)一下攀隔!
無(wú)論是百度地圖、高德地圖栖榨、谷歌地圖還是騰訊地圖它們都有自己的SDK,我們只需要在自己的工程中導(dǎo)入SDK并查看相應(yīng)的官方文檔明刷,基本上就可以實(shí)現(xiàn)導(dǎo)航婴栽。但是這樣每個(gè)地圖的SDK都導(dǎo)入不但麻煩而且占用APP的內(nèi)存。最關(guān)鍵的是我們上傳到AppStore的包文件是有限制的辈末。所以我的原則是能不導(dǎo)入的SDK 就不導(dǎo)入愚争。
-
還有一種方式就是是以URI跳轉(zhuǎn)的方式(在iOS中就是以URL Scheme的方式),直接跳到對(duì)應(yīng)的地圖APP中挤聘,直接利用對(duì)方的功能來(lái)導(dǎo)航轰枝。缺點(diǎn)是用戶(hù)沒(méi)有安裝對(duì)應(yīng)的APP就不能使用其進(jìn)行導(dǎo)航。 點(diǎn)擊導(dǎo)航按鈕會(huì)出現(xiàn)如下的彈窗组去, 當(dāng)然手機(jī)上未安裝的地圖 其名稱(chēng)就不會(huì)出現(xiàn)在彈窗上鞍陨。
-
在 iOS9之后 若想用URI方式跳轉(zhuǎn)到百度地圖、高德地圖从隆、騰訊地圖诚撵、谷歌地圖,需要你在info.plist加入這些東西键闺。(ps:LSApplicationQueriesSchemes寿烟,短的自己手打吧,另外注意類(lèi)型P猎铩)
以下出行的默認(rèn)方式都是駕車(chē)
一筛武、百度地圖
- 說(shuō)到百度地圖,就不得不說(shuō)它很坑爹挎塌。因?yàn)榘俣鹊貓D獲取的經(jīng)緯度徘六,是在GCJ-02(火星坐標(biāo))進(jìn)行偏移得到的經(jīng)緯度,而高德勃蜘、谷歌硕噩、騰訊都是使用GCJ-02坐標(biāo)體系得到的經(jīng)緯度。這樣使用百度地圖獲取到的經(jīng)緯度在高德缭贡、谷歌炉擅、騰訊上導(dǎo)航都會(huì)出現(xiàn)很大的偏差辉懒。所以自己做的APP中需要地圖功能最好不要導(dǎo)入百度地圖的SDK(使用上面三個(gè)中任何一個(gè)地圖獲取到的經(jīng)緯度都可以很容易的轉(zhuǎn)換成百度地圖需要的經(jīng)緯度),如果你是像我這樣中途接手的項(xiàng)目谍失,百度地圖的相應(yīng)功能已經(jīng)做好了眶俩,那你可以用下面的方式換算一下經(jīng)緯度(最下方)。
- 代碼如下 :需傳入起點(diǎn)和終點(diǎn)的經(jīng)緯度
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]) {
UIAlertAction *baiduMapAction = [UIAlertAction actionWithTitle:@"百度地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *baiduParameterFormat = @"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:終點(diǎn)&mode=driving";
NSString *urlString = [[NSString stringWithFormat:
baiduParameterFormat,
userLocation.location.coordinate.latitude,
userLocation.location.coordinate.longitude,
self.destinationCoordinate.latitude,
self.destinationCoordinate.longitude]
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[actionSheet addAction:baiduMapAction];
}
- 各個(gè)參數(shù)代表的含義可參考百度地圖官方文檔快鱼。
二颠印、高德地圖
- 只需傳入終點(diǎn)經(jīng)緯度 高德地圖能夠跳轉(zhuǎn)回你的APP,前提是backScheme=%@(你的APP的URL)要填寫(xiě)抹竹。代碼如下
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://map/"]]) {
UIAlertAction *gaodeMapAction = [UIAlertAction actionWithTitle:@"高德地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *gaodeParameterFormat = @"iosamap://navi?sourceApplication=%@&backScheme=%@&poiname=%@&lat=%f&lon=%f&dev=1&style=2";
NSString *urlString = [[NSString stringWithFormat:
gaodeParameterFormat,
@"yourAppName",
@"yourAppUrlSchema",
@"終點(diǎn)",
self.destinationCoordinate.latitude,
self.destinationCoordinate.longitude]
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[actionSheet addAction:gaodeMapAction];
}
- 各個(gè)參數(shù)的含義可參考高德地圖官方文檔
三线罕、蘋(píng)果地圖
- 需傳入起點(diǎn)和終點(diǎn)的經(jīng)緯度,并導(dǎo)入頭文件#import MapKit/MKMapItem.h>
[actionSheet addAction:[UIAlertAction actionWithTitle:@"蘋(píng)果地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//起點(diǎn)
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
CLLocationCoordinate2D desCorrdinate = CLLocationCoordinate2DMake(self.destinationCoordinate.latitude, self.destinationCoordinate.longitude);
//終點(diǎn)
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:desCorrdinate addressDictionary:nil]];
//默認(rèn)駕車(chē)
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey:[NSNumber numberWithInteger:MKMapTypeStandard],
MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
}]];
- 各個(gè)參數(shù)的含義可參考蘋(píng)果地圖官方文檔
四窃判、谷歌地圖
- 只需傳入終點(diǎn)的經(jīng)緯度
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://map/"]]) {
[actionSheet addAction:[UIAlertAction actionWithTitle:@"蘋(píng)果地圖"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",
appName,
urlScheme,
coordinate.latitude,
coordinate.longitude]
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}]];
}
- 各個(gè)參數(shù)的含義可參考谷歌地圖官方文檔
五钞楼、騰訊地圖
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://map/"]]) {
[actionSheet addAction:[UIAlertAction actionWithTitle:@"騰訊地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *QQParameterFormat = @"qqmap://map/routeplan?type=drive&fromcoord=%f, %f&tocoord=%f,%f&coord_type=1&policy=0&refer=%@";
NSString *urlString = [[NSString stringWithFormat:
QQParameterFormat,
userLocation.location.coordinate.latitude,
userLocation.location.coordinate.longitude,
self.destinationCoordinate.latitude,
self.destinationCoordinate.longitude,
@"yourAppName"]
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}]];
}
- 各個(gè)參數(shù)的含義可參考騰訊地圖官方文檔
GCJ-02坐標(biāo)轉(zhuǎn)換成BD-09坐標(biāo) 和逆轉(zhuǎn)換
- GCJ-02坐標(biāo)轉(zhuǎn)換為BD-09坐標(biāo)
/** * 將GCJ-02坐標(biāo)轉(zhuǎn)換為BD-09坐標(biāo) 即將高德地圖上獲取的坐標(biāo)轉(zhuǎn)換成百度坐標(biāo) */
- (CLLocationCoordinate2D)gcj02CoordianteToBD09:(CLLocationCoordinate2D)gdCoordinate
{
double x_PI = M_PI * 3000.0 /180.0;
double gd_lat = gdCoordinate.latitude;
double gd_lon = gdCoordinate.longitude;
double z = sqrt(gd_lat * gd_lat + gd_lon * gd_lon) + 0.00002 * sin(gd_lat * x_PI);
double theta = atan2(gd_lat, gd_lon) + 0.000003 * cos(gd_lon * x_PI);
return CLLocationCoordinate2DMake(z * sin(theta) + 0.006, z * cos(theta) + 0.0065);
}
- BD-09坐標(biāo)轉(zhuǎn)換為GCJ-02坐標(biāo)
/** * 將BD-09坐標(biāo)轉(zhuǎn)換為GCJ-02坐標(biāo) 即將百度地圖上獲取的坐標(biāo)轉(zhuǎn)換成高德地圖的坐標(biāo) */
- (CLLocationCoordinate2D)bd09CoordinateToGCJ02:(CLLocationCoordinate2D)bdCoordinate
{
double x_PI = M_PI * 3000.0 /180.0;
double bd_lat = bdCoordinate.latitude - 0.006;
double bd_lon = bdCoordinate.longitude - 0.0065;
double z = sqrt(bd_lat * bd_lat + bd_lon * bd_lon) - 0.00002 * sin(bd_lat * x_PI);
double theta = atan2(bd_lat, bd_lon) - 0.000003 * cos(bd_lon * x_PI);
return CLLocationCoordinate2DMake(z * sin(theta), z * cos(theta));
}
地圖坐標(biāo)系轉(zhuǎn)換
#import <CoreLocation/CoreLocation.h>
/*
從 CLLocationManager 取出來(lái)的經(jīng)緯度放到 mapView 上顯示,是錯(cuò)誤的!
從 CLLocationManager 取出來(lái)的經(jīng)緯度去 Google Maps API 做逆地址解析袄琳,當(dāng)然是錯(cuò)的询件!
從 MKMapView 取出來(lái)的經(jīng)緯度去 Google Maps API 做逆地址解析終于對(duì)了。去百度地圖API做逆地址解析唆樊,依舊是錯(cuò)的宛琅!
從上面兩處取的經(jīng)緯度放到百度地圖上顯示都是錯(cuò)的!錯(cuò)的逗旁!的嘿辟!
分為 地球坐標(biāo),火星坐標(biāo)(iOS mapView 高德 痢艺, 國(guó)內(nèi)google ,搜搜仓洼、阿里云 都是火星坐標(biāo)),百度坐標(biāo)(百度地圖數(shù)據(jù)主要都是四維圖新提供的)
火星坐標(biāo): MKMapView
地球坐標(biāo): CLLocationManager
當(dāng)用到CLLocationManager 得到的數(shù)據(jù)轉(zhuǎn)化為火星坐標(biāo), MKMapView不用處理
API 坐標(biāo)系
百度地圖API 百度坐標(biāo)
騰訊搜搜地圖API 火星坐標(biāo)
搜狐搜狗地圖API 搜狗坐標(biāo)
阿里云地圖API 火星坐標(biāo)
圖吧MapBar地圖API 圖吧坐標(biāo)
高德MapABC地圖API 火星坐標(biāo)
靈圖51ditu地圖API 火星坐標(biāo)
*/
@interface CLLocation (Location)
//從地圖坐標(biāo)轉(zhuǎn)化到火星坐標(biāo)
- (CLLocation *)locationMarsFromEarth;
//從火星坐標(biāo)轉(zhuǎn)化到百度坐標(biāo)
- (CLLocation *)locationBaiduFromMars;
//從百度坐標(biāo)到火星坐標(biāo)
- (CLLocation *)locationMarsFromBaidu;
//從火星坐標(biāo)到地圖坐標(biāo)
- (CLLocation *)locationEarthFromMars;
//從百度坐標(biāo)到地圖坐標(biāo)
- (CLLocation *)locationEarthFromBaidu;
@end
#import "CLLocation+Location.h"
void transform_earth_from_mars(double lat, double lng, double* tarLat, double* tarLng);
void transform_mars_from_baidu(double lat, double lng, double* tarLat, double* tarLng);
void transform_baidu_from_mars(double lat, double lng, double* tarLat, double* tarLng);
@implementation CLLocation (Location)
- (CLLocation*)locationMarsFromEarth
{
double lat = 0.0;
double lng = 0.0;
transform_earth_from_mars(self.coordinate.latitude, self.coordinate.longitude, &lat, &lng);
return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat+self.coordinate.latitude, lng+self.coordinate.longitude)
altitude:self.altitude
horizontalAccuracy:self.horizontalAccuracy
verticalAccuracy:self.verticalAccuracy
course:self.course
speed:self.speed
timestamp:self.timestamp];
}
- (CLLocation*)locationEarthFromMars
{
double lat = 0.0;
double lng = 0.0;
transform_earth_from_mars(self.coordinate.latitude, self.coordinate.longitude, &lat, &lng);
return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(self.coordinate.latitude-lat, self.coordinate.longitude-lng)
altitude:self.altitude
horizontalAccuracy:self.horizontalAccuracy
verticalAccuracy:self.verticalAccuracy
course:self.course
speed:self.speed
timestamp:self.timestamp];
return nil;
}
- (CLLocation*)locationBaiduFromMars
{
double lat = 0.0;
double lng = 0.0;
transform_mars_from_baidu(self.coordinate.latitude, self.coordinate.longitude, &lat, &lng);
return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat, lng)
altitude:self.altitude
horizontalAccuracy:self.horizontalAccuracy
verticalAccuracy:self.verticalAccuracy
course:self.course
speed:self.speed
timestamp:self.timestamp];
}
- (CLLocation*)locationMarsFromBaidu
{
double lat = 0.0;
double lng = 0.0;
transform_baidu_from_mars(self.coordinate.latitude, self.coordinate.longitude, &lat, &lng);
return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat, lng)
altitude:self.altitude
horizontalAccuracy:self.horizontalAccuracy
verticalAccuracy:self.verticalAccuracy
course:self.course
speed:self.speed
timestamp:self.timestamp];
}
-(CLLocation*)locationEarthFromBaidu
{
double lat = 0.0;
double lng = 0.0;
CLLocation *Mars = [self locationMarsFromBaidu];
transform_earth_from_mars(Mars.coordinate.latitude, Mars.coordinate.longitude, &lat, &lng);
return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(Mars.coordinate.latitude-lat, Mars.coordinate.longitude-lng)
altitude:self.altitude
horizontalAccuracy:self.horizontalAccuracy
verticalAccuracy:self.verticalAccuracy
course:self.course
speed:self.speed
timestamp:self.timestamp];
return nil;
}
@end
// --- transform_earth_from_mars ---
// 參考來(lái)源:https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#353936
// Krasovsky 1940
//
// a = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// ee = (a^2 - b^2) / a^2;
const double a = 6378245.0;
const double ee = 0.00669342162296594323;
bool transform_sino_out_china(double lat, double lon)
{
if (lon < 72.004 || lon > 137.8347)
return true;
if (lat < 0.8293 || lat > 55.8271)
return true;
return false;
}
double transform_earth_from_mars_lat(double x, double y)
{
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x));
ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
ret += (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0;
ret += (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
return ret;
}
double transform_earth_from_mars_lng(double x, double y)
{
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x));
ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
ret += (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0;
ret += (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0;
return ret;
}
void transform_earth_from_mars(double lat, double lng, double* tarLat, double* tarLng)
{
if (transform_sino_out_china(lat, lng))
{
*tarLat = lat;
*tarLng = lng;
return;
}
double dLat = transform_earth_from_mars_lat(lng - 105.0, lat - 35.0);
double dLon = transform_earth_from_mars_lng(lng - 105.0, lat - 35.0);
double radLat = lat / 180.0 * M_PI;
double magic = sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * M_PI);
dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * M_PI);
*tarLat = dLat;
*tarLng = dLon;
}
// --- transform_earth_from_mars end ---
// --- transform_mars_vs_bear_paw ---
// 參考來(lái)源:http://blog.woodbunny.com/post-68.html
const double x_pi = M_PI * 3000.0 / 180.0;
void transform_mars_from_baidu(double gg_lat, double gg_lon, double *bd_lat, double *bd_lon)
{
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;
}
void transform_baidu_from_mars(double bd_lat, double bd_lon, double *gg_lat, double *gg_lon)
{
double x = bd_lon - 0.0065, y = bd_lat - 0.006;
double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
*gg_lon = z * cos(theta);
*gg_lat = z * sin(theta);
}
- Tips:無(wú)論導(dǎo)入的是百度SDK還是高德SDK堤舒,他們內(nèi)部都封裝了將目標(biāo)經(jīng)緯度轉(zhuǎn)換為高德坐標(biāo)系或百度坐標(biāo)系(文檔上的接口可能被棄用沒(méi)有及時(shí)更新色建,是不是很坑爹),但是沒(méi)有將高德或百度坐標(biāo)轉(zhuǎn)換為別的坐標(biāo)系下的坐標(biāo)的接口舌缤。
- 設(shè)置URL Scheme:http://blog.csdn.net/wm9028/article/details/49995329