| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?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;
- }
- /**
- * 批量计算多个起点到一个终点的距离 (高德 V3 距离测量接口)
- * @param array $origins 起点坐标数组,例如:[['lng' => '116.481028', 'lat' => '39.989643'], ...]
- * @param string $toLnt 终点经度
- * @param string $toLat 终点纬度
- * @param int $type 路径计算的方式和方法 (0:直线距离, 1:驾车, 3:步行)
- * @return array
- */
- public static function calculateBatchDistance($origins, $toLnt, $toLat, $type = 1)
- {
- if (empty($origins)) {
- return [];
- }
- // 将起点数组拼接成高德要求的格式: lng1,lat1|lng2,lat2
- $originStrs = [];
- foreach ($origins as $origin) {
- $originStrs[] = $origin['lng'] . ',' . $origin['lat'];
- }
- $originsParam = implode('|', $originStrs);
- $destinationParam = $toLnt . ',' . $toLat;
- $domain = getenv('YII_ENV') == 'production' ? 'https://restapi.amap.com' : 'http://restapi.amap.com';
- $url = $domain . "/v3/distance?key=" . self::$thirdMapKey . "&origins={$originsParam}&destination={$destinationParam}&type={$type}";
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $result = \yii\helpers\Json::decode($result);
- $distances = [];
- if (!empty($result['status']) && $result['status'] == 1 && !empty($result['results'])) {
- foreach ($result['results'] as $index => $res) {
- // 返回的结果顺序与传入的 origins 顺序一致
- $distances[$index] = $res['distance'] ?? 0;
- }
- }
-
- return $distances;
- }
- //逆地址解析,根据经纬度解析出中文地址 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;
- }
- }
|