码迷,mamicode.com
首页 > 其他好文 > 详细

GPS坐标转化

时间:2015-03-18 13:58:09      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

//将GPS纬度信息转换为“火星坐标”的纬度
function latToGcj02(lon, lat) {
    var c = Math.PI,
        d = Math.sin,
        e = -100 + 2 * lon + 3 * lat + 0.2 * lat * lat + 0.1 * lon * lat + 0.2 * Math.sqrt(Math.abs(lon)),
        e = e + 2 * (20 * d(6 * lon * c) + 20 * d(2 * lon * c)) / 3,
        e = e + 2 * (20 * d(lat * c) + 40 * d(lat / 3 * c)) / 3;
    return e += 2 * (160 * d(lat / 12 * c) + 320 * d(lat * c / 30)) / 3
}

//将GPS经度信息转换为“火星坐标”的纬度
function lonToGcj02(lon, lat) {
    var c = Math.PI,
        d = Math.sin,
        e = 300 + lon + 2 * lat + 0.1 * lon * lon + 0.1 * lon * lat + 0.1 * Math.sqrt(Math.abs(lon)),
        e = e + 2 * (20 * d(6 * lon * c) + 20 * d(2 * lon * c)) / 3,
        e = e + 2 * (20 * d(lon * c) + 40 * d(lon / 3 * c)) / 3;
    return e += 2 * (150 * d(lon / 12 * c) +
        300 * d(lon / 30 * c)) / 3
}

//将经纬度信息转换为“火星坐标”体系
function GPSToGcj02(lon, lat) {
    var a = 6378245,
        ee = 0.006693421622965823;

    var d = Math.PI,
        e = lonToGcj02(lon - 105, lat - 35),
        f = latToGcj02(lon - 105, lat - 35),
        h = lat / 180 * d,
        k = Math.sin(h),
        k = 1 - ee * k * k,
        l = Math.sqrt(k),
        e = 180 * e / (a / l * Math.cos(h) * d),
        f = 180 * f / (a * (1 - ee) / (k * l) * d);
    return [lon + e, lat + f]
}

//例子
navigator.geolocation.getCurrentPosition(function(e) {
    console.log(GPSToGcj02(e.coords.longitude, e.coords.latitude));
}, function() {

}, {
    enableHighAccuracy: true,
    timeout : 10000
});

 

GPS坐标转化

标签:

原文地址:http://www.cnblogs.com/sherlock99/p/4346906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!