# 手机端获取手机设备GPS坐标，php转换为GC102标准坐标（火星坐标）

``````<?php
// \$lon = '113.86696624756';
// \$lat = '35.293231964111';
\$lon = '113.867041';
\$lat = '35.293326';

\$a = 6378245.0;
\$ee = 0.00669342162296594323;
\$pi = pi();
\$res = gps84_To_Gcj02(\$lat, \$lon);

var_dump(\$res);

function gps84_To_Gcj02(\$lat, \$lon)
{
\$a = 6378245.0;
\$ee = 0.00669342162296594323;
\$pi = pi();
\$dLat = transformLat(\$lon - 105.0, \$lat - 35.0);
\$dLon = transformLon(\$lon - 105.0, \$lat - 35.0);
\$radLat = \$lat / 180.0 * \$pi;
\$magic = 1 - \$ee * \$magic * \$magic;
\$sqrtMagic = sqrt(\$magic);

\$dLat = (\$dLat * 180.0) / ((\$a * (1 - \$ee)) / (\$magic * \$sqrtMagic) * \$pi);
\$dLon = (\$dLon * 180.0) / (\$a / \$sqrtMagic * cos(\$radLat) * \$pi);
\$mgLat = \$lat + \$dLat;
\$mgLon = \$lon + \$dLon;
return array(\$mgLat, \$mgLon);
}

function transformLat(\$x, \$y)
{
\$pi = pi();
\$ret = -100.0 + 2.0 * \$x + 3.0 * \$y + 0.2 * \$y * \$y + 0.1 * \$x * \$y
+ 0.2 * sqrt(abs(\$x));
\$ret += (20.0 * sin(6.0 * \$x * \$pi) + 20.0 * sin(2.0 * \$x * \$pi)) * 2.0 / 3.0;
\$ret += (20.0 * sin(\$y * \$pi) + 40.0 * sin(\$y / 3.0 * \$pi)) * 2.0 / 3.0;
\$ret += (160.0 * sin(\$y / 12.0 * \$pi) + 320 * sin(\$y * \$pi / 30.0)) * 2.0 / 3.0;
return \$ret;
}

function transformLon(\$x, \$y)
{
\$pi = pi();
\$ret = 300.0 + \$x + 2.0 * \$y + 0.1 * \$x * \$x + 0.1 * \$x * \$y + 0.1
* sqrt(abs(\$x));
\$ret += (20.0 * sin(6.0 * \$x * \$pi) + 20.0 * sin(2.0 * \$x * \$pi)) * 2.0 / 3.0;
\$ret += (20.0 * sin(\$x * \$pi) + 40.0 * sin(\$x / 3.0 * \$pi)) * 2.0 / 3.0;
\$ret += (150.0 * sin(\$x / 12.0 * \$pi) + 300.0 * sin(\$x / 30.0
* \$pi)) * 2.0 / 3.0;
return \$ret;
}

``````