对跳转高德,百度,苹果,google,腾讯地图导航进行工具类封装
#import <Foundation/Foundation.h>
@interface GQJumpMapNavTool : NSObject
/**
* 从指定地导航到指定地
*
*/
- (void)showMapNavigationViewFormcurrentLatitude:(double)currentLatitude currentLongitute:(double)currentLongitute currentName:(NSString *)currentName TotargetLatitude:(double)targetLatitude targetLongitute:(double)targetLongitute toName:(NSString *)toName;
/**
* 从目前位置导航到指定地
*
*/
- (void)showMapNavigationViewWithtargetLatitude:(double)targetLatitude targetLongitute:(double)targetLongitute toName:(NSString *)name;
@end
#import "GQJumpMapNavTool.h"
#import "CLLocation+YCLocation.h"
#import <MapKit/MapKit.h>
@implementation GQJumpMapNavTool
{
double _currentLatitude;
double _currentLongitute;
double _targetLatitude;
double _targetLongitute;
NSString *_currentName;
NSString *_toName;
}
- (void)showMapNavigationViewWithtargetLatitude:(double)targetLatitude targetLongitute:(double)targetLongitute toName:(NSString *)name{
[self showMapNavigationViewFormcurrentLatitude:Loca_latitude currentLongitute:Loca_longitude currentName:Loca_Address TotargetLatitude:targetLatitude targetLongitute:targetLongitute toName:name];
}
- (void)showMapNavigationViewFormcurrentLatitude:(double)currentLatitude currentLongitute:(double)currentLongitute currentName:(NSString *)currentName TotargetLatitude:(double)targetLatitude targetLongitute:(double)targetLongitute toName:(NSString *)toName{
CLLocation *from = [[CLLocation alloc]initWithLatitude:currentLatitude longitude:currentLongitute];
CLLocation *fromLoction = [from locationMarsFromEarth];
_currentLatitude = fromLoction.coordinate.latitude;
_currentLongitute = fromLoction.coordinate.longitude;
_currentName = currentName;
_targetLatitude = targetLatitude;
_targetLongitute = targetLongitute;
_toName = toName;
NSArray *mapArr= [self checkHasOwnApp];
GQActionSheetView *mapNavView = [[GQActionSheetView alloc] initWithTitles:mapArr withCancleTitle:@"取消" cancelColor:kBlueColor];
mapNavView.choosedBlock = ^(NSString *text, NSInteger idx) {
NSString *btnTitle = mapArr[idx];
if ([btnTitle isEqualToString:@"苹果地图"]) {
CLLocationCoordinate2D from = CLLocationCoordinate2DMake(_currentLatitude, _currentLongitute);
MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:from addressDictionary:nil]];
currentLocation.name = _currentName;
//终点
CLLocationCoordinate2D to = CLLocationCoordinate2DMake(_targetLatitude, _targetLongitute);
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:to addressDictionary:nil]];
NSLog(@"网页google地图:%f,%f",to.latitude,to.longitude);
toLocation.name = _currentName;
NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation, nil];
NSDictionary *options = @{
MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey:
[NSNumber numberWithInteger:MKMapTypeStandard],
MKLaunchOptionsShowsTrafficKey:@YES
};
//打开苹果自身地图应用
[MKMapItem openMapsWithItems:items launchOptions:options];
}
else if ([btnTitle isEqualToString:@"google地图"]) {
NSString *urlStr = [NSString stringWithFormat:@"comgooglemaps://?saddr=%.8f,%.8f&daddr=%.8f,%.8f&directionsmode=transit",_currentLatitude,_currentLongitute,_targetLatitude,_targetLongitute];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
}else if ([btnTitle isEqualToString:@"高德地图"]){
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=%f&slon=%f&sname=%@&did=BGVIS2&dlat=%f&dlon=%f&dname=%@&dev=0&m=0&t=0",_currentLatitude,_currentLongitute,_currentName,_targetLatitude,_targetLongitute,_toName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *r = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:r];
}else if ([btnTitle isEqualToString:@"腾讯地图"]){
NSString *urlStr = [[NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&fromcoord=%f,%f&from=%@&tocoord=%f,%f&to=%@&policy=1",_currentLatitude,_currentLongitute,_currentName,_targetLatitude,_targetLongitute,_toName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *r = [NSURL URLWithString:urlStr];
[[UIApplication sharedApplication] openURL:r];
}else if([btnTitle isEqualToString:@"百度地图"])
{
CLLocation *from = [[CLLocation alloc]initWithLatitude:_currentLatitude longitude:_currentLongitute];
CLLocation *fromLoction = [from locationBaiduFromMars];
CLLocation *to = [[CLLocation alloc]initWithLatitude:_targetLatitude longitude:_targetLongitute];
CLLocation *toLoction = [to locationBaiduFromMars];
NSString *stringURL = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:%@&destination=latlng:%f,%f|name:%@&mode=driving&coord_type=gcj02",fromLoction.coordinate.latitude,fromLoction.coordinate.longitude,_currentName,toLoction.coordinate.latitude,toLoction.coordinate.longitude,_toName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
}
};
[mapNavView show];
}
- (NSArray *)checkHasOwnApp{
NSArray *mapSchemeArr = @[@"comgooglemaps://",@"iosamap://navi",@"baidumap://map/",@"qqmap://"];
NSMutableArray *appListArr = [[NSMutableArray alloc] initWithObjects:@"苹果地图", nil];
for (int i = 0; i < [mapSchemeArr count]; i++) {
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[mapSchemeArr objectAtIndex:i]]]]) {
if (i == 0) {
[appListArr addObject:@"google地图"];
}else if (i == 1){
[appListArr addObject:@"高德地图"];
}else if (i == 2){
[appListArr addObject:@"百度地图"];
}else if (i == 3){
[appListArr addObject:@"腾讯地图"];
}
}
}
return appListArr;
}
@end
#import <CoreLocation/CoreLocation.h>
@interface CLLocation (YCLocation)
//从地图坐标转化到火星坐标
- (CLLocation*)locationMarsFromEarth;
//从火星坐标转化到百度坐标
- (CLLocation*)locationBaiduFromMars;
//从百度坐标到火星坐标
- (CLLocation*)locationMarsFromBaidu;
@end
```objc
#import "CLLocation+YCLocation.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 (YCLocation)
- (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, lng)
altitude:self.altitude
horizontalAccuracy:self.horizontalAccuracy
verticalAccuracy:self.verticalAccuracy
course:self.course
speed:self.speed
timestamp:self.timestamp];
}
- (CLLocation*)locationEarthFromMars
{
// 二分法查纠偏文件
// http://xcodev.com/131.html
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];
}
@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(abs(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(abs(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 = lat + dLat;
*tarLng = lng + 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);
}
@end