公司App集成的是百度地圖,然后調(diào)起百度地圖、高德地圖畏梆、騰訊地圖您宪、蘋果地圖進行導航的功能.
一.首先需要在info.plist文件中添加白名單如下:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>baidumap</string> //百度
<string>iosamap</string> //高德
<string>qqmap</string> //騰訊
</array>
使用: [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"白名單://"]]
判斷是否安裝白名單里面的App.
二.由于項目繼承的是百度地圖,調(diào)用其他導航會有地理偏差,我們要進行坐標轉(zhuǎn)換,可以使用這個轉(zhuǎn)換工具.
第一種:調(diào)起蘋果自帶的地圖進行導航:
蘋果自帶的地圖不需要設(shè)置白名單,需要設(shè)置終點的經(jīng)緯度就行,需要導入#import <MapKit/MapKit.h>
頭文件
//坐標轉(zhuǎn)換
CLLocationCoordinate2D gcj02Coord = [JZLocationConverter bd09ToGcj02:loc];
//用戶位置
MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
//終點位置
MKMapItem *toLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:gcj02Coord addressDictionary:nil] ];
toLocation.name = title;
NSArray *items = @[currentLoc,toLocation];
NSDictionary *dic = @{
MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
MKLaunchOptionsShowsTrafficKey : @(YES)
};
[MKMapItem openMapsWithItems:items launchOptions:dic];
第二種:調(diào)起百度地圖進行導航:
可以參考:百度地圖官方文檔 實現(xiàn)如下:
//百度地圖
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name:%@&mode=driving&coord_type=bd09ll",modelData.coordinate.latitude,modelData.coordinate.longitude, modelData.titleName] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: urlString]];
}
參數(shù)設(shè)置可以參考文檔
第三種:調(diào)起高德地圖進行導航:
可以參考:高德地圖官方文檔 實現(xiàn)如下:
CLLocationCoordinate2D gcj02Coord = [JZLocationConverter bd09ToGcj02:modelData.coordinate];
float shopLat = gcj02Coord.latitude;
float shoplng = gcj02Coord.longitude;
//高德地圖
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&backScheme=%@&sname=我的位置&dev=1&t=0&dlat=%f&dlon=%f&dname=%@&dev=0",@"AppName",@"url schemes",shopLat,shoplng, modelData.titleName] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: urlString]];
}
第四種:調(diào)起騰訊地圖進行導航:
可以參考:騰訊地圖官方文檔 實現(xiàn)如下:
//騰訊地圖
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];
NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%f,%f&to=%@",shopLat,shoplng, modelData.titleName] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: urlString]];
}
下面是地理坐標轉(zhuǎn)換工具:
.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface LocationConverter : NSObject
/**
* @brief 世界標準地理坐標(WGS-84) 轉(zhuǎn)換成 中國國測局地理坐標(GCJ-02)<火星坐標>
*
* ####只在中國大陸的范圍的坐標有效,以外直接返回世界標準坐標
*
* @param location 世界標準地理坐標(WGS-84)
*
* @return 中國國測局地理坐標(GCJ-02)<火星坐標>
*/
+ (CLLocationCoordinate2D)wgs84ToGcj02:(CLLocationCoordinate2D)location;
/**
* @brief 中國國測局地理坐標(GCJ-02) 轉(zhuǎn)換成 世界標準地理坐標(WGS-84)
*
* ####此接口有1-2米左右的誤差奠涌,需要精確定位情景慎用
*
* @param location 中國國測局地理坐標(GCJ-02)
*
* @return 世界標準地理坐標(WGS-84)
*/
+ (CLLocationCoordinate2D)gcj02ToWgs84:(CLLocationCoordinate2D)location;
/**
* @brief 世界標準地理坐標(WGS-84) 轉(zhuǎn)換成 百度地理坐標(BD-09)
*
* @param location 世界標準地理坐標(WGS-84)
*
* @return 百度地理坐標(BD-09)
*/
+ (CLLocationCoordinate2D)wgs84ToBd09:(CLLocationCoordinate2D)location;
/**
* @brief 中國國測局地理坐標(GCJ-02)<火星坐標> 轉(zhuǎn)換成 百度地理坐標(BD-09)
*
* @param location 中國國測局地理坐標(GCJ-02)<火星坐標>
*
* @return 百度地理坐標(BD-09)
*/
+ (CLLocationCoordinate2D)gcj02ToBd09:(CLLocationCoordinate2D)location;
/**
* @brief 百度地理坐標(BD-09) 轉(zhuǎn)換成 中國國測局地理坐標(GCJ-02)<火星坐標>
*
* @param location 百度地理坐標(BD-09)
*
* @return 中國國測局地理坐標(GCJ-02)<火星坐標>
*/
+ (CLLocationCoordinate2D)bd09ToGcj02:(CLLocationCoordinate2D)location;
/**
* @brief 百度地理坐標(BD-09) 轉(zhuǎn)換成 世界標準地理坐標(WGS-84)
*
* ####此接口有1-2米左右的誤差宪巨,需要精確定位情景慎用
*
* @param location 百度地理坐標(BD-09)
*
* @return 世界標準地理坐標(WGS-84)
*/
+ (CLLocationCoordinate2D)bd09ToWgs84:(CLLocationCoordinate2D)location;
@end
.m
#import "LocationConverter.h"
#import <CoreLocation/CoreLocation.h>
#define LAT_OFFSET_0(x,y) -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x))
#define LAT_OFFSET_1 (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0
#define LAT_OFFSET_2 (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0
#define LAT_OFFSET_3 (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0
#define LON_OFFSET_0(x,y) 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x))
#define LON_OFFSET_1 (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0
#define LON_OFFSET_2 (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0
#define LON_OFFSET_3 (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0
#define RANGE_LON_MAX 137.8347
#define RANGE_LON_MIN 72.004
#define RANGE_LAT_MAX 55.8271
#define RANGE_LAT_MIN 0.8293
// jzA = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// ee = (a^2 - b^2) / a^2;
#define jzA 6378245.0
#define jzEE 0.00669342162296594323
@implementation LocationConverter
+ (double)transformLat:(double)x bdLon:(double)y
{
double ret = LAT_OFFSET_0(x, y);
ret += LAT_OFFSET_1;
ret += LAT_OFFSET_2;
ret += LAT_OFFSET_3;
return ret;
}
+ (double)transformLon:(double)x bdLon:(double)y
{
double ret = LON_OFFSET_0(x, y);
ret += LON_OFFSET_1;
ret += LON_OFFSET_2;
ret += LON_OFFSET_3;
return ret;
}
+ (BOOL)outOfChina:(double)lat bdLon:(double)lon
{
if (lon < RANGE_LON_MIN || lon > RANGE_LON_MAX)
return true;
if (lat < RANGE_LAT_MIN || lat > RANGE_LAT_MAX)
return true;
return false;
}
+ (CLLocationCoordinate2D)gcj02Encrypt:(double)ggLat bdLon:(double)ggLon
{
CLLocationCoordinate2D resPoint;
double mgLat;
double mgLon;
if ([self outOfChina:ggLat bdLon:ggLon]) {
resPoint.latitude = ggLat;
resPoint.longitude = ggLon;
return resPoint;
}
double dLat = [self transformLat:(ggLon - 105.0)bdLon:(ggLat - 35.0)];
double dLon = [self transformLon:(ggLon - 105.0) bdLon:(ggLat - 35.0)];
double radLat = ggLat / 180.0 * M_PI;
double magic = sin(radLat);
magic = 1 - jzEE * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((jzA * (1 - jzEE)) / (magic * sqrtMagic) * M_PI);
dLon = (dLon * 180.0) / (jzA / sqrtMagic * cos(radLat) * M_PI);
mgLat = ggLat + dLat;
mgLon = ggLon + dLon;
resPoint.latitude = mgLat;
resPoint.longitude = mgLon;
return resPoint;
}
+ (CLLocationCoordinate2D)gcj02Decrypt:(double)gjLat gjLon:(double)gjLon {
CLLocationCoordinate2D gPt = [self gcj02Encrypt:gjLat bdLon:gjLon];
double dLon = gPt.longitude - gjLon;
double dLat = gPt.latitude - gjLat;
CLLocationCoordinate2D pt;
pt.latitude = gjLat - dLat;
pt.longitude = gjLon - dLon;
return pt;
}
+ (CLLocationCoordinate2D)bd09Decrypt:(double)bdLat bdLon:(double)bdLon
{
CLLocationCoordinate2D gcjPt;
double x = bdLon - 0.0065, y = bdLat - 0.006;
double z = sqrt(x * x + y * y) - 0.00002 * sin(y * M_PI);
double theta = atan2(y, x) - 0.000003 * cos(x * M_PI);
gcjPt.longitude = z * cos(theta);
gcjPt.latitude = z * sin(theta);
return gcjPt;
}
+(CLLocationCoordinate2D)bd09Encrypt:(double)ggLat bdLon:(double)ggLon
{
CLLocationCoordinate2D bdPt;
double x = ggLon, y = ggLat;
double z = sqrt(x * x + y * y) + 0.00002 * sin(y * M_PI);
double theta = atan2(y, x) + 0.000003 * cos(x * M_PI);
bdPt.longitude = z * cos(theta) + 0.0065;
bdPt.latitude = z * sin(theta) + 0.006;
return bdPt;
}
+ (CLLocationCoordinate2D)wgs84ToGcj02:(CLLocationCoordinate2D)location
{
return [self gcj02Encrypt:location.latitude bdLon:location.longitude];
}
+ (CLLocationCoordinate2D)gcj02ToWgs84:(CLLocationCoordinate2D)location
{
return [self gcj02Decrypt:location.latitude gjLon:location.longitude];
}
+ (CLLocationCoordinate2D)wgs84ToBd09:(CLLocationCoordinate2D)location
{
CLLocationCoordinate2D gcj02Pt = [self gcj02Encrypt:location.latitude
bdLon:location.longitude];
return [self bd09Encrypt:gcj02Pt.latitude bdLon:gcj02Pt.longitude] ;
}
+ (CLLocationCoordinate2D)gcj02ToBd09:(CLLocationCoordinate2D)location
{
return [self bd09Encrypt:location.latitude bdLon:location.longitude];
}
+ (CLLocationCoordinate2D)bd09ToGcj02:(CLLocationCoordinate2D)location
{
return [self bd09Decrypt:location.latitude bdLon:location.longitude];
}
+ (CLLocationCoordinate2D)bd09ToWgs84:(CLLocationCoordinate2D)location
{
CLLocationCoordinate2D gcj02 = [self bd09ToGcj02:location];
return [self gcj02Decrypt:gcj02.latitude gjLon:gcj02.longitude];
}
@end