从纬度和经度显示地址

show address from latitude and long latitude

我有这样的显示经纬度的查询:

Latitude : <?=$post_search['lat'];?>
Long Latitude : <?=$post_search['long'];?>

示例结果:

Latitude : -7.966620399999998
Long Latitude : 112.63263210000002

我如何创建一个函数来显示基于查询的地址:

<?=$post_search['lat'];?>
<?=$post_search['long'];?>

我的代码使用这个库:see in pastebin

我不知道你的图书馆。但它没有任何库。 您应该使用 googleapis 地理编码。

查询很简单

http://maps.googleapis.com/maps/api/geocode/json?latlng=44.4647452,7.3553838&sensor=true

你需要在你的请求中设置 lat lng 和 运行 这个,请求后你解析 json.

php 的示例:

const GOOGLE_MAPS_URL = 'http://maps.googleapis.com/maps/api/geocode/json';

function getUrlLatLng( $latLng) {
   $queryString =  http_build_query(['latlng' => $latLng,'sensor'=> true]);
   return GOOGLE_MAPS_URL . '?' .  $queryString;
}

function getData($lat, $lng) {
    $latLng = implode(',',[$lat, $lng]);
    $url = getUrlLatLng($latLng);
    return json_decode(file_get_contents($url)); 
}

function parseAddress($result) {

    return ($result[0]->formatted_address); //this parse address ass field you need;
}

function getAddress($lat, $lng) {
   $address = (array)@getData($lat, $lng);

   if(empty((array)@$address['results'])) {
     throw new Exception('Not result'); 
   }

   return $address['results'];
}

function getFullAddress($lat, $lng) {
   $address = getAddress($lat, $lng);
   return parseAddress($address);
}

$result = getFullAddress(-7.966620399999998, 112.63263210000002);

echo $result;