|
|
@@ -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)) {
|