shish 2 kuukautta sitten
vanhempi
commit
2c510542e1
2 muutettua tiedostoa jossa 94 lisäystä ja 1 poistoa
  1. 14 1
      app-hd/controllers/MapController.php
  2. 80 0
      common/components/mapUtil.php

+ 14 - 1
app-hd/controllers/MapController.php

@@ -9,7 +9,7 @@ use Yii;
 class MapController extends BaseController
 {
 
-    public $guestAccess = ['suggestion','resolve'];
+    public $guestAccess = ['suggestion','resolve','nearby'];
 
     //腾讯地图关键词输入提醒 ssh 2020.1.21
     public function actionSuggestion()
@@ -36,4 +36,17 @@ class MapController extends BaseController
         util::success($result);
     }
 
+    //附近地址(高德逆地理 POI 列表)
+    public function actionNearby()
+    {
+        $lat = Yii::$app->request->get('lat');
+        $long = Yii::$app->request->get('long');
+        $locationType = Yii::$app->request->get('locationType', 'gcj02');
+        if (empty($lat) || empty($long)) {
+            util::success(['list' => []]);
+        }
+        $list = mapUtil::resolveNearby($lat, $long, $locationType);
+        util::success(['list' => $list]);
+    }
+
 }

+ 80 - 0
common/components/mapUtil.php

@@ -172,6 +172,86 @@ class mapUtil
         ];
     }
 
+    /**
+     * 根据经纬度获取当前位置及附近 POI 列表(高德逆地理 extensions=all)
+     */
+    public static function resolveNearby($lat, $long, $locationType = 'gcj02')
+    {
+        $lat = (float)$lat;
+        $long = (float)$long;
+        if ($lat == 0 || $long == 0) {
+            return [];
+        }
+
+        $domain = getenv('YII_ENV') == 'production' ? 'https://restapi.amap.com' : 'http://restapi.amap.com';
+        $location = $long . ',' . $lat;
+        $url = $domain . '/v3/geocode/regeo?location=' . urlencode($location)
+            . '&key=' . self::$thirdMapKey
+            . '&extensions=all&radius=1000&roadlevel=0';
+        if ($locationType === 'wgs84' || $locationType === 'gps') {
+            $url .= '&coordsys=gps';
+        }
+
+        $curl = new curl\Curl();
+        $result = Json::decode($curl->get($url));
+        if (empty($result['status']) || (string)$result['status'] !== '1' || empty($result['regeocode'])) {
+            return [];
+        }
+
+        $regeo = $result['regeocode'];
+        $component = $regeo['addressComponent'] ?? [];
+        $province = self::normalizeAmapRegionValue($component['province'] ?? '');
+        $city = self::normalizeAmapRegionValue($component['city'] ?? '');
+        $district = self::normalizeAmapRegionValue($component['district'] ?? '');
+        if (empty($city)) {
+            $city = $province;
+        }
+
+        $centerAddress = $regeo['formatted_address'] ?? '';
+        $centerName = self::normalizeAmapRegionValue($component['township'] ?? '');
+        if (empty($centerName)) {
+            $centerName = '当前位置';
+        }
+
+        $list = [[
+            'name' => $centerName,
+            'address' => $centerAddress,
+            'province' => $province,
+            'city' => $city,
+            'district' => $district,
+            'location' => $long . ',' . $lat,
+            'showAddress' => $centerAddress,
+        ]];
+
+        $pois = $regeo['pois'] ?? [];
+        $exists = [$centerName];
+        foreach ($pois as $poi) {
+            $poiName = $poi['name'] ?? '';
+            if ($poiName === '' || in_array($poiName, $exists, true)) {
+                continue;
+            }
+            $exists[] = $poiName;
+            $poiAddress = $poi['address'] ?? '';
+            if ($poiAddress === '') {
+                $poiAddress = $centerAddress ? ($centerAddress . ' ' . $poiName) : $poiName;
+            }
+            $list[] = [
+                'name' => $poiName,
+                'address' => $poiAddress,
+                'province' => $province,
+                'city' => $city,
+                'district' => $district,
+                'location' => $poi['location'] ?? ($long . ',' . $lat),
+                'showAddress' => $poiAddress,
+            ];
+            if (count($list) >= 21) {
+                break;
+            }
+        }
+
+        return $list;
+    }
+
     private static function normalizeAmapRegionValue($value)
     {
         if (is_array($value)) {