GPS国际坐标(WGS-84)转火星坐标(GCJ-02)----golang版本

package main

 

import (

    "fmt"

    "math"

)

 

// GCJEncrypt ..

func GCJEncrypt(wgsLat, wgsLon float64) (float64, float64) {

    if wgsLon < 72.004 || wgsLon > 137.8347 || wgsLat < 0.8293 || wgsLat > 55.8271 {

        return wgsLat, wgsLon

    }

    PI := 3.14159265358979324

    a := 6378245.0 // a: 卫星椭球坐标投影到平面地图坐标系的投影因子。

    ee := 0.00669342162296594323 // ee: 椭球的偏心率。

    var x = wgsLon - 105.0

    var y = wgsLat - 35.0

    var ret = -100.0 + 2.0*x + 3.0*y + 0.2*y*y + 0.1*x*y + 0.2*math.Sqrt(math.Abs(x))

    ret += (20.0*math.Sin(6.0*x*PI) + 20.0*math.Sin(2.0*x*PI)) * 2.0 / 3.0

    ret += (20.0*math.Sin(y*PI) + 40.0*math.Sin(y/3.0*PI)) * 2.0 / 3.0

    ret += (160.0*math.Sin(y/12.0*PI) + 320*math.Sin(y*PI/30.0)) * 2.0 / 3.0

    var dLat = ret

    x = wgsLon - 105.0

    y = wgsLat - 35.0

    ret = 300.0 + x + 2.0*y + 0.1*x*x + 0.1*x*y + 0.1*math.Sqrt(math.Abs(x))

    ret += (20.0*math.Sin(6.0*x*PI) + 20.0*math.Sin(2.0*x*PI)) * 2.0 / 3.0

    ret += (20.0*math.Sin(x*PI) + 40.0*math.Sin(x/3.0*PI)) * 2.0 / 3.0

    ret += (150.0*math.Sin(x/12.0*PI) + 300.0*math.Sin(x/30.0*PI)) * 2.0 / 3.0

    var dLon = ret

    var radLat = wgsLat / 180.0 * PI

    var magic = math.Sin(radLat)

    magic = 1 - ee*magic*magic

    var sqrtMagic = math.Sqrt(magic)

    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI)

    dLon = (dLon * 180.0) / (a / sqrtMagic * math.Cos(radLat) * PI)

    return wgsLat + dLat, wgsLon + dLon

}

 

func main() {

    fmt.Println(GCJEncrypt(22.585152, 113.913083))

}

 

你可能感兴趣的:(GCJ-02,火星坐标)