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]; } }