136 lines
5.2 KiB
Dart
136 lines
5.2 KiB
Dart
|
//
|
||
|
// FATWGS84ConvertToGCJ02ForAMapView.m
|
||
|
// FinApplet
|
||
|
//
|
||
|
// Created by 杨涛 on 2018/8/9.
|
||
|
// Copyright © 2018年 finogeeks. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#import "FATWGS84ConvertToGCJ02.h"
|
||
|
#define RANGE_LON_MAX 137.8347
|
||
|
#define RANGE_LON_MIN 72.004
|
||
|
#define RANGE_LAT_MAX 55.8271
|
||
|
#define RANGE_LAT_MIN 0.8293
|
||
|
#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 jzA 6378245.0
|
||
|
#define jzEE 0.00669342162296594323
|
||
|
|
||
|
static const double fat_a = 6378245.0;
|
||
|
static const double fat_ee = 0.00669342162296594323;
|
||
|
|
||
|
@implementation FATWGS84ConvertToGCJ02ForAMapView
|
||
|
+ (CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc {
|
||
|
CLLocationCoordinate2D adjustLoc;
|
||
|
if ([self isLocationOutOfChina:wgsLoc]) {
|
||
|
adjustLoc = wgsLoc;
|
||
|
} else {
|
||
|
double adjustLat = [self transformLatWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
|
||
|
double adjustLon = [self transformLonWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
|
||
|
double radLat = wgsLoc.latitude / 180.0 * M_PI;
|
||
|
double magic = sin(radLat);
|
||
|
magic = 1 - fat_ee * magic * magic;
|
||
|
double sqrtMagic = sqrt(magic);
|
||
|
adjustLat = (adjustLat * 180.0) / ((fat_a * (1 - fat_ee)) / (magic * sqrtMagic) * M_PI);
|
||
|
adjustLon = (adjustLon * 180.0) / (fat_a / sqrtMagic * cos(radLat) * M_PI);
|
||
|
adjustLoc.latitude = wgsLoc.latitude + adjustLat - 0.00039900; // 减去这个数字 完全是凑数,准确性有待验证
|
||
|
adjustLoc.longitude = wgsLoc.longitude + adjustLon;
|
||
|
}
|
||
|
return adjustLoc;
|
||
|
}
|
||
|
|
||
|
+ (CLLocationCoordinate2D)transformFromGCJToWGS:(CLLocationCoordinate2D)wgsLoc {
|
||
|
return [self gcj02Decrypt:wgsLoc.latitude gjLon:wgsLoc.longitude];
|
||
|
}
|
||
|
|
||
|
+ (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)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;
|
||
|
}
|
||
|
|
||
|
+ (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;
|
||
|
}
|
||
|
|
||
|
+ (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)isLocationOutOfChina:(CLLocationCoordinate2D)location {
|
||
|
if (location.longitude < 72.004 || location.longitude > 137.8347 || location.latitude < 0.8293 || location.latitude > 55.8271)
|
||
|
return YES;
|
||
|
return NO;
|
||
|
}
|
||
|
|
||
|
+ (double)transformLatWithX:(double)x withY:(double)y {
|
||
|
double lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x));
|
||
|
lat += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
|
||
|
lat += (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0;
|
||
|
lat += (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
|
||
|
return lat;
|
||
|
}
|
||
|
|
||
|
+ (double)transformLonWithX:(double)x withY:(double)y {
|
||
|
double lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x));
|
||
|
lon += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
|
||
|
lon += (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0;
|
||
|
lon += (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0;
|
||
|
return lon;
|
||
|
}
|
||
|
|
||
|
@end
|