1. 程式人生 > >地圖座標糾偏及投影轉換的常見演算法

地圖座標糾偏及投影轉換的常見演算法

Web墨卡託座標與WGS84座標互轉

//經緯度轉墨卡託
-(CGPoint )lonLat2Mercator:(CGPoint ) lonLat
{
    CGPoint  mercator;
    double x = lonLat.x *20037508.34/180;
    double y = log(tan((90+lonLat.y)*M_PI/360))/(M_PI/180);
    y = y *20037508.34/180;
    mercator.x = x;
    mercator.y = y;
    return mercator ;
}
//墨卡託轉經緯度
-(CGPoint )Mercator2lonLat:(CGPoint ) mercator
{
    CGPoint lonLat;
    double x = mercator.x/20037508.34*180;
    double y = mercator.y/20037508.34*180;
    y= 180/M_PI*(2*atan(exp(y*M_PI/180))-M_PI/2);
    lonLat.x = x;
    lonLat.y = y;
    return lonLat;
}

火星座標系 (GCJ-02) 與百度座標系 (BD-09) 互轉
#include <math.h>  
  
const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;  
//將 GCJ-02 座標轉換成 BD-09 座標
void bd_encrypt(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 bd_decrypt(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);  
}  

簡單演算法(lat和lng是經緯度,球面座標):

//To_B是轉到百度,To_G是轉到GCJ-02。

var TO_BLNG = function(lng){return lng+0.0065;};

var TO_BLAT = function(lat){return lat+0.0060;};

var TO_GLNG = function(lng){return lng-0.0065;};

var TO_GLAT = function(lat){return lat-0.0060;};

GPS轉換為GCJ-02座標

https://www.google.com.hk/search?q=wgtochina_lb&hl=zh-CN


參考資料:
  1. http://bbs.esrichina-bj.cn/esri/viewthread.php?tid=78245
  2. http://blog.csdn.net/coolypf/article/details/8569813
  3. http://www.cnblogs.com/milkmap/p/3627940.html
  4. http://www.cnblogs.com/milkmap/p/3768379.html