diff --git a/app/Http/Controllers/Api/Hr/SignController.php b/app/Http/Controllers/Api/Hr/SignController.php index a5351cf..411e303 100644 --- a/app/Http/Controllers/Api/Hr/SignController.php +++ b/app/Http/Controllers/Api/Hr/SignController.php @@ -15,6 +15,7 @@ use Illuminate\Http\Response; use Illuminate\Support\Facades\DB; use Illuminate\Validation\Rule; use Slowlyo\OwlAdmin\Services\AdminSettingService; +use coordtransform\Helper as CoordinateHelper; /** * 考勤打卡 @@ -80,12 +81,11 @@ class SignController extends Controller $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']; - // } + $locationType = $request->input('location_type', 'wgs84'); + if ($locationType == 'wgs84') { + list($lon, $lat) = CoordinateHelper::wgs84togcj02($lon, $lat); + list($lon, $lat) = CoordinateHelper::gcj02tobd09($lon, $lat); + } // 计算距离 $distance = $service->haversineDistance( $lat, @@ -142,12 +142,11 @@ class SignController extends Controller $lon = $request->input('position.lon'); $lat = $request->input('position.lat'); // 切换坐标系 - // $locationType = $request->input('position.location_type', 'wgs84'); - // if ($locationType == 'wgs84') { - // $res = wgs84ToGcj02($lat, $lon); - // $lon = $res['lon']; - // $lat = $res['lat']; - // } + $locationType = $request->input('location_type', 'wgs84'); + if ($locationType == 'wgs84') { + list($lon, $lat) = CoordinateHelper::wgs84togcj02($lon, $lat); + list($lon, $lat) = CoordinateHelper::gcj02tobd09($lon, $lat); + } $distance = $service->haversineDistance( $lat, $lon, diff --git a/bootstrap/helpers.php b/bootstrap/helpers.php index 2a56744..97016f2 100644 --- a/bootstrap/helpers.php +++ b/bootstrap/helpers.php @@ -59,31 +59,3 @@ function delta($lat, $lon) return ['lat' => $dLat, 'lon' => $dLon]; } - -function wgs84ToGcj02($lat, $lon) -{ - if (outOfChina($lat, $lon)) { - return ['lat' => $lat, 'lon' => $lon]; - } - $d = delta($lat, $lon); - - return ['lat' => $lat + $d['lat'], 'lon' => $lon + $d['lon']]; -} - -function gcj02ToWgs84($lat, $lon) -{ - if (outOfChina($lat, $lon)) { - return ['lat' => $lat, 'lon' => $lon]; - } - $d = delta($lat, $lon); - - return ['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; -// } diff --git a/composer.json b/composer.json index 8522d13..9424754 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,7 @@ "require": { "php": "^8.1", "alphasnow/aliyun-oss-laravel": "^4.7", + "billy-poon/coordtransform": "^1.0", "guzzlehttp/guzzle": "^7.2", "laravel/framework": "^10.10", "laravel/sanctum": "^3.3", diff --git a/composer.lock b/composer.lock index 61aa110..6e389c5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e50204833e31b832f0bb8b3a5438e5ab", + "content-hash": "0d1bf59d3ee7e094a8d73020bc09799d", "packages": [ { "name": "aliyuncs/oss-sdk-php", @@ -141,6 +141,61 @@ ], "time": "2022-12-03T14:27:07+00:00" }, + { + "name": "billy-poon/coordtransform", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/billy-poon/coordtransform.git", + "reference": "2f4454dae3a3515e33bbe3885490581f0ce29e6a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/billy-poon/coordtransform/zipball/2f4454dae3a3515e33bbe3885490581f0ce29e6a", + "reference": "2f4454dae3a3515e33bbe3885490581f0ce29e6a", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "php": ">=5.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "coordtransform\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Billy Poon", + "email": "panqbi@yeah.net", + "homepage": "https://github.com/billy-poon" + } + ], + "description": "一个提供了百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换的工具模块。移植自:https://github.com/wandergis/coordtransform/", + "homepage": "https://github.com/billy-poon/coordtransform", + "keywords": [ + "bd09", + "coordinate", + "gcj02", + "transform", + "wgs84" + ], + "support": { + "issues": "https://github.com/billy-poon/coordtransform/issues", + "source": "https://github.com/billy-poon/coordtransform/tree/master" + }, + "time": "2018-08-21T17:33:38+00:00" + }, { "name": "brick/math", "version": "0.11.0", diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php index b700079..b165af3 100644 --- a/tests/Feature/ExampleTest.php +++ b/tests/Feature/ExampleTest.php @@ -3,8 +3,13 @@ namespace Tests\Feature; // use Illuminate\Foundation\Testing\RefreshDatabase; + +use App\Admin\Services\EmployeeSignService; +use App\Admin\Services\StoreService; use Tests\TestCase; +use coordtransform\Helper as CoordinateHelper; + class ExampleTest extends TestCase { /** @@ -12,6 +17,20 @@ class ExampleTest extends TestCase */ public function test_the_application_returns_a_successful_response(): void { - dump(wgs84ToGcj02('29.421605', '106.524698')); + // GPS 坐标 + $lon = '106.524698'; + $lat = '29.421605'; + + // 百度接口转换结果 + // $lon = '106.53499612144785'; + // $lat = '29.424741925277104'; + + list($lon, $lat) = CoordinateHelper::wgs84togcj02($lon, $lat); + list($lon, $lat) = CoordinateHelper::gcj02tobd09($lon, $lat); + + // BD-09 坐标 + $lon2 = '106.534967'; + $lat2 = '29.424352'; + dump(EmployeeSignService::make()->haversineDistance($lat, $lon, $lat2, $lon2)); } }