| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- /**
- * User: ssh
- * Date: 2019/11/20
- * Time: 23:21
- */
- namespace common\components;
- use linslin\yii2\curl;
- use yii\helpers\Json;
- class mapUtil
- {
- public static $thirdMapKey = 'fe792548aa6e15ef4d5d501e508eaeae';
- public static function suggestion($region, $keyword)
- {
- if (getenv('YII_ENV') == 'production') {
- $url = "https://restapi.amap.com/v3/assistant/inputtips?city={$region}&keywords={$keyword}&key=" . self::$thirdMapKey;
- } else {
- $url = "http://restapi.amap.com/v3/assistant/inputtips?city={$region}&keywords={$keyword}&key=" . self::$thirdMapKey;
- }
- $curl = new curl\Curl();
- $result = $curl->get($url);
- return Json::decode($result);
- }
- //计算距离 lng经度 lat纬度 ssh 2020.5.14
- public static function calculateDistance($fromLnt, $fromLat, $toLnt, $toLat)
- {
- if (getenv('YII_ENV') == 'production') {
- $url = "https://restapi.amap.com/v5/direction/electrobike?key=" . self::$thirdMapKey . '&origin=' . $fromLnt . ',' . $fromLat . '&destination=' . $toLnt . ',' . $toLat;
- } else {
- $url = "http://restapi.amap.com/v5/direction/electrobike?key=" . self::$thirdMapKey . '&origin=' . $fromLnt . ',' . $fromLat . '&destination=' . $toLnt . ',' . $toLat;
- }
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $result = Json::decode($result);
- $distance = 0;
- if (!empty($result['status']) && $result['status'] == 1) {
- $path = $result['route']['paths'] ?? [];
- $current = $path[0] ?? [];
- $distance = $current['distance'] ?? 0;
- }
- return $distance;
- }
- //逆地址解析,根据经纬度解析出中文地址 ssh
- public static function resolveLocation($lat, $long)
- {
- $url = "https://apis.map.qq.com/ws/geocoder/v1/?location={$lat},{$long}&get_poi=0&key=" . self::$thirdMapKey;
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $result = Json::decode($result);
- return $result;
- }
- /**
- * 高德地图坐标转换为腾讯地图坐标
- * 虽然高德和腾讯都使用GCJ-02坐标系,但可能存在细微偏差
- * 此方法提供精确转换
- * @param float $lng 高德地图经度
- * @param float $lat 高德地图纬度
- * @return array ['lng' => 转换后经度, 'lat' => 转换后纬度]
- */
- public static function convertAmapToTencent($lng, $lat)
- {
- // 高德转腾讯的偏移参数(基于实际测试调整)
- $dlng = -0.0065;
- $dlat = -0.006;
-
- // 应用偏移量
- $convertedLng = $lng + $dlng;
- $convertedLat = $lat + $dlat;
-
- return [
- 'lng' => round($convertedLng, 6),
- 'lat' => round($convertedLat, 6)
- ];
- }
- /**
- * 使用腾讯地图API进行坐标转换
- * 从高德坐标系(GCJ02)转换为腾讯坐标系
- * @param float $lng 原始经度
- * @param float $lat 原始纬度
- * @return array ['lng' => 转换后经度, 'lat' => 转换后纬度]
- */
- public static function convertCoordinateByApi($lng, $lat)
- {
- // 使用腾讯地图坐标转换API
- $url = "https://apis.map.qq.com/ws/coord/v1/translate";
- $params = [
- 'locations' => $lat . ',' . $lng,
- 'type' => 3, // 3表示从GCJ02转换为腾讯坐标
- 'key' => self::$thirdMapKey
- ];
-
- $curl = new curl\Curl();
- $result = $curl->get($url . '?' . http_build_query($params));
- $result = Json::decode($result);
-
- if ($result['status'] == 0 && !empty($result['locations'])) {
- $location = $result['locations'][0];
- return [
- 'lng' => round($location['lng'], 6),
- 'lat' => round($location['lat'], 6)
- ];
- }
-
- // 如果API转换失败,使用默认转换方法
- //return self::convertAmapToTencent($lng, $lat);
- return ['lng'=>$lng, 'lat'=>$lat];
- }
- }
|