generated from liutk/owl-admin-base
切换坐标系 gcj02
parent
91efa6220a
commit
5af75cc9d3
|
|
@ -113,7 +113,7 @@ class StoreController extends AdminController
|
|||
->showSteps(false)
|
||||
->required(),
|
||||
amis()->InputCityControl()->name('region')->label(__('store.region'))->allowDistrict(false)->extractValue(false)->required(),
|
||||
amis()->LocationControl()->name('location')->label(__('store.location'))->ak(config('baidu.js_secret'))->autoSelectCurrentLoc()->required(),
|
||||
amis()->LocationControl()->name('location')->label(__('store.location'))->coordinatesType('gcj02')->ak(config('baidu.js_secret'))->autoSelectCurrentLoc()->required(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -74,10 +74,18 @@ class SignController extends Controller
|
|||
|
||||
// 如果开启定位
|
||||
if ($request->filled(['lon', 'lat'])) {
|
||||
$lon = $request->input('lon');
|
||||
$lat = $request->input('lat');
|
||||
$locationType = $request->input('location_type', 'wgs84');
|
||||
if ($locationType == 'wgs84') {
|
||||
$res = wgs84ToGcj02($lat, $lon);
|
||||
$lon = $res['lon'];
|
||||
$lat = $res['lat'];
|
||||
}
|
||||
// 计算距离
|
||||
$distance = $service->haversineDistance(
|
||||
$request->input('lat'),
|
||||
$request->input('lon'),
|
||||
$lat,
|
||||
$lon,
|
||||
$store->lat,
|
||||
$store->lon,
|
||||
);
|
||||
|
|
|
|||
|
|
@ -23,3 +23,56 @@ if (! function_exists('trim_zeros')) {
|
|||
return $var === '' ? '0' : $var;
|
||||
}
|
||||
}
|
||||
|
||||
function transformLat($x, $y) {
|
||||
$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) {
|
||||
$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;
|
||||
}
|
||||
|
||||
function delta($lat, $lon) {
|
||||
$a = 6378245.0;
|
||||
$ee = 0.00669342162296594323;
|
||||
$dLat = transformLat($lon - 105.0, $lat - 35.0);
|
||||
$dLon = transformLon($lon - 105.0, $lat - 35.0);
|
||||
$radLat = $lat / 180.0 * pi();
|
||||
$magic = sin($radLat);
|
||||
$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());
|
||||
return array('lat' => $dLat, 'lon' => $dLon);
|
||||
}
|
||||
|
||||
function wgs84ToGcj02($lat, $lon) {
|
||||
if (outOfChina($lat, $lon)) {
|
||||
return array('lat' => $lat, 'lon' => $lon);
|
||||
}
|
||||
$d = delta($lat, $lon);
|
||||
return array('lat' => $lat + $d['lat'], 'lon' => $lon + $d['lon']);
|
||||
}
|
||||
|
||||
function gcj02ToWgs84($lat, $lon) {
|
||||
if (outOfChina($lat, $lon)) {
|
||||
return array('lat' => $lat, 'lon' => $lon);
|
||||
}
|
||||
$d = delta($lat, $lon);
|
||||
return array('lat' => $lat - $d['lat'], 'lon' => $lon - $d['lon']);
|
||||
}
|
||||
|
||||
function outOfChina($lat, $lon) {
|
||||
return ($lon < 72.004 || $lon > 137.8347) || ($lat < 0.8293 || $lat > 55.8271);
|
||||
}
|
||||
// function pi() {
|
||||
// return 3.14159265358979323846;
|
||||
// }
|
||||
|
|
|
|||
Loading…
Reference in New Issue