GaodeMaps.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Utils;
  3. class GaodeMaps
  4. {
  5. const AK = '5bc6af7f78ce0e866ba5e680f0b05f3d';
  6. /**
  7. * 坐标逆向解析 参考地址 https://lbs.amap.com/api/webservice/guide/api/georegeo#regeo
  8. *
  9. * @param $lngLat array 经纬度
  10. *
  11. * @return $address 解析地址
  12. * */
  13. public static function getAddress(array $lngLat){
  14. $url = "https://restapi.amap.com/v3/geocode/regeo?output=json&location=".$lngLat[0].",".$lngLat[1]."&key=".self::AK."&radius=100&extensions=all";
  15. $res = curl_request($url);
  16. $res = json_decode($res);
  17. $address = '';
  18. if($res->info == "OK"){
  19. $address = $res->regeocode->formatted_address;
  20. }
  21. return $address;
  22. }
  23. /**
  24. * 批量请求
  25. *
  26. *
  27. * */
  28. public static function batchRequest(array $ops){
  29. $url = "https://restapi.amap.com/v3/batch?key=".self::AK;
  30. $method = "post";
  31. $data = json_encode($ops);
  32. $res = curl_request($url,$method,$data);
  33. return $res;
  34. }
  35. /**
  36. * 批量坐标逆向解析
  37. * */
  38. public static function getAddressMany(array $arr){
  39. $params = [];
  40. foreach ($arr as $v){
  41. $params[] =['url'=>"/v3/geocode/regeo?output=json&location=".(empty($v) ? 0 : $v->lng) .",".(empty($v) ? 0 : $v->lat) ."&key=".self::AK."&radius=100&extensions=all"];
  42. }
  43. $res = self::batchRequest(['ops'=>$params]);
  44. return $res;
  45. }
  46. }