最近在做導航,所以把自己找到的資料總結一下!
無論是百度地圖、高德地圖、谷歌地圖還是騰訊地圖它們都有自己的SDK,我們只需要在自己的工程中導入SDK并查看相應的官方文檔,基本上就可以實現導航。但是這樣每個地圖的SDK都導入不但麻煩而且占用APP的內存。最關鍵的是我們上傳到AppStore的包文件是有限制的。所以我的原則是能不導入的SDK 就不導入。
-
還有一種方式就是是以URI跳轉的方式(在iOS中就是以URL Scheme的方式),直接跳到對應的地圖APP中,直接利用對方的功能來導航。缺點是用戶沒有安裝對應的APP就不能使用其進行導航。 點擊導航按鈕會出現如下的彈窗, 當然手機上未安裝的地圖 其名稱就不會出現在彈窗上。
1439521824220516.png -
在 iOS9之后 若想用URI方式跳轉到百度地圖、高德地圖、騰訊地圖、谷歌地圖,需要你在info.plist加入這些東西。(ps:LSApplicationQueriesSchemes,短的自己手打吧,另外注意類型?。?/p>
20160422164514276.png
以下出行的默認方式都是駕車
一、百度地圖
- 說到百度地圖,就不得不說它很坑爹。因為百度地圖獲取的經緯度,是在GCJ-02(火星坐標)進行偏移得到的經緯度,而高德、谷歌、騰訊都是使用GCJ-02坐標體系得到的經緯度。這樣使用百度地圖獲取到的經緯度在高德、谷歌、騰訊上導航都會出現很大的偏差。所以自己做的APP中需要地圖功能最好不要導入百度地圖的SDK(使用上面三個中任何一個地圖獲取到的經緯度都可以很容易的轉換成百度地圖需要的經緯度),如果你是像我這樣中途接手的項目,百度地圖的相應功能已經做好了,那你可以用下面的方式換算一下經緯度(最下方)。
- 代碼如下 :需傳入起點和終點的經緯度
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:終點&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];
}
- 各個參數代表的含義可參考百度地圖官方文檔。
二、高德地圖
- 只需傳入終點經緯度 高德地圖能夠跳轉回你的APP,前提是backScheme=%@(你的APP的URL)要填寫。代碼如下
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",
@"終點",
self.destinationCoordinate.latitude,
self.destinationCoordinate.longitude]
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[actionSheet addAction:gaodeMapAction];
}
- 各個參數的含義可參考高德地圖官方文檔
三、蘋果地圖
- 需傳入起點和終點的經緯度,并導入頭文件#import MapKit/MKMapItem.h>
[actionSheet addAction:[UIAlertAction actionWithTitle:@"蘋果地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//起點
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
CLLocationCoordinate2D desCorrdinate = CLLocationCoordinate2DMake(self.destinationCoordinate.latitude, self.destinationCoordinate.longitude);
//終點
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:desCorrdinate addressDictionary:nil]];
//默認駕車
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey:[NSNumber numberWithInteger:MKMapTypeStandard],
MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
}]];
- 各個參數的含義可參考蘋果地圖官方文檔
四、谷歌地圖
- 只需傳入終點的經緯度
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://map/"]]) {
[actionSheet addAction:[UIAlertAction actionWithTitle:@"蘋果地圖"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]];
}]];
}
- 各個參數的含義可參考谷歌地圖官方文檔
五、騰訊地圖
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]];
}]];
}
- 各個參數的含義可參考騰訊地圖官方文檔
GCJ-02坐標轉換成BD-09坐標 和逆轉換
- GCJ-02坐標轉換為BD-09坐標
/** * 將GCJ-02坐標轉換為BD-09坐標 即將高德地圖上獲取的坐標轉換成百度坐標 */
- (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坐標轉換為GCJ-02坐標
/** * 將BD-09坐標轉換為GCJ-02坐標 即將百度地圖上獲取的坐標轉換成高德地圖的坐標 */
- (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));
}
地圖坐標系轉換
#import <CoreLocation/CoreLocation.h>
/*
從 CLLocationManager 取出來的經緯度放到 mapView 上顯示,是錯誤的!
從 CLLocationManager 取出來的經緯度去 Google Maps API 做逆地址解析,當然是錯的!
從 MKMapView 取出來的經緯度去 Google Maps API 做逆地址解析終于對了。去百度地圖API做逆地址解析,依舊是錯的!
從上面兩處取的經緯度放到百度地圖上顯示都是錯的!錯的!的!
分為 地球坐標,火星坐標(iOS mapView 高德 , 國內google ,搜搜、阿里云 都是火星坐標),百度坐標(百度地圖數據主要都是四維圖新提供的)
火星坐標: MKMapView
地球坐標: CLLocationManager
當用到CLLocationManager 得到的數據轉化為火星坐標, MKMapView不用處理
API 坐標系
百度地圖API 百度坐標
騰訊搜搜地圖API 火星坐標
搜狐搜狗地圖API 搜狗坐標
阿里云地圖API 火星坐標
圖吧MapBar地圖API 圖吧坐標
高德MapABC地圖API 火星坐標
靈圖51ditu地圖API 火星坐標
*/
@interface CLLocation (Location)
//從地圖坐標轉化到火星坐標
- (CLLocation *)locationMarsFromEarth;
//從火星坐標轉化到百度坐標
- (CLLocation *)locationBaiduFromMars;
//從百度坐標到火星坐標
- (CLLocation *)locationMarsFromBaidu;
//從火星坐標到地圖坐標
- (CLLocation *)locationEarthFromMars;
//從百度坐標到地圖坐標
- (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 ---
// 參考來源: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 ---
// 參考來源: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:無論導入的是百度SDK還是高德SDK,他們內部都封裝了將目標經緯度轉換為高德坐標系或百度坐標系(文檔上的接口可能被棄用沒有及時更新,是不是很坑爹),但是沒有將高德或百度坐標轉換為別的坐標系下的坐標的接口。
- 設置URL Scheme:http://blog.csdn.net/wm9028/article/details/49995329