PHP程序员

<?php
/**
 * 高德坐标转GPS坐标,误差5-8米左右
 * @link https://blog.make3.cn
 * @author: BillZhang <708947679@qq.com>
 * @version V1.0.0
 * @Date: 2019/6/18 2:40 PM
 */
class LatLonPoint{
    private $longitude=0;
    private $latitude=0;
    public function __construct($latitude,$longitude)
    {
        $this->longitude = $longitude;
        $this->latitude  = $latitude;
    }

    public function getLatitude(){
        return $this->latitude;
    }
    public function getLongitude(){
        return $this->longitude;
    }
}

class gaodeToGps
{
    private static $a = 6378245.0; // 长半轴
    private static $pi = 3.14159265358979324;  // π
    private static $ee = 0.00669342162296594323; // e²

    /**
     * GCJ-02 to WGS-84
     */
    public static function toGPSPoint($latitude,$longitude){
        $dev = self::calDev($latitude, $longitude);
        $retLat = $latitude - $dev->getLatitude();
        $retLon = $longitude - $dev->getLongitude();
        for ($i = 0; $i < 1; $i++) {
            $dev = self::calDev($retLat, $retLon);
            $retLat = $latitude - $dev->getLatitude();
            $retLon = $longitude - $dev->getLongitude();
        }
        return new LatLonPoint($retLat, $retLon);
    }

    /**
     * 计算偏差
     */
    private static function calDev($wgLat,$wgLon){
        if (self::isOutOfChina($wgLat, $wgLon)) {
            return new LatLonPoint(0, 0);
        }
         $dLat = self::calLat($wgLon - 105.0, $wgLat - 35.0);
         $dLon = self::calLon($wgLon - 105.0, $wgLat - 35.0);
         $radLat = $wgLat / 180.0 * self::$pi;
         $magic = sin($radLat);
         $magic = 1 - self::$ee * $magic * $magic;
         $sqrtMagic = sqrt($magic);
         $dLat = ($dLat * 180.0) / ((self::$a * (1 - self::$ee)) / ($magic * $sqrtMagic) * self::$pi);
         $dLon = ($dLon * 180.0) / (self::$a / $sqrtMagic * cos($radLat) * self::$pi);
        return new LatLonPoint($dLat, $dLon);
    }

    /**
     * 判断是否在国外
     */
    private static function isOutOfChina($lat,$lon){
        if ($lon < 72.004 || $lon > 137.8347)
            return true;
        if ($lat < 0.8293 || $lat > 55.8271)
            return true;
        return false;
    }

    /**
     * 计算纬度
     */
    private static function calLat($x,$y){
        $resultLat = -100.0 + 2.0 * $x + 3.0 * $y + 0.2 * $y * $y + 0.1 * $x * $y
            + 0.2 * sqrt(abs($x));
        $resultLat += (20.0 * sin(6.0 * $x * self::$pi) + 20.0 * sin(2.0 * $x * self::$pi)) * 2.0 / 3.0;
        $resultLat += (20.0 * sin($y * self::$pi) + 40.0 * sin($y / 3.0 * self::$pi)) * 2.0 / 3.0;
        $resultLat += (160.0 * sin($y / 12.0 * self::$pi) + 320 * sin($y * self::$pi / 30.0)) * 2.0 / 3.0;
        return $resultLat;
    }

    /**
     * 计算经度
     */
    private static function calLon($x,$y){
        $resultLon = 300.0 + $x + 2.0 * $y + 0.1 * $x * $x + 0.1 * $x * $y + 0.1
            * sqrt(abs($x));
        $resultLon += (20.0 * sin(6.0 * $x * self::$pi) + 20.0 * sin(2.0 * $x * self::$pi)) * 2.0 / 3.0;
        $resultLon += (20.0 * sin($x * self::$pi) + 40.0 * sin($x / 3.0 * self::$pi)) * 2.0 / 3.0;
        $resultLon += (150.0 * sin($x / 12.0 * self::$pi) + 300.0 * sin($x / 30.0
                    * self::$pi)) * 2.0 / 3.0;
        return $resultLon;
    }
}

$aa = gaodeToGps::toGPSPoint('30.6844384765625','103.95760064019098');
echo "Latitude:".$aa->getLatitude();
echo "\n";
echo "Longitude:".$aa->getLongitude();
echo "\n";
评论
...

小明

有来过,666666。

4楼  2017-08-08  23:15发表