PHP纬度经度来解决

Joe*_*aca 0 php location geolocation latitude-longitude

我的网站上有一个表格,用户输入地址.当他们提交表单时,我将此位置转换为纬度/经度并将其存储在MySQL数据库中.我正在使用Google的Geocode服务进行此转换.问题是我无法找到将纬度/经度转换回地址的类或服务,据我所知,谷歌的地理编码服务是单向转换.我意识到我可以将物理地址存储在数据库中,但随着时间的推移,当它可以以更简单的格式存储时,这是浪费的空间.有没有人知道从纬度/经度转换为地址的类/服务,或者我是否错了,我可以使用谷歌的地理编码系统?过去几天我找了答案但找不到任何东西.谢谢你的帮助!

And*_*ann 8

将地理坐标转换为地址称为反向地理编码.在此脚本中,我们使用Google地图API,因为它是免费的,快速的,无需API密钥.

谷歌的速率限制地理编码每天每个IP有2500个API调用.

用于Reveres Geocoding的PHP函数

<?
  function getaddress($lat,$lng)
  {
     $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
     $json = @file_get_contents($url);
     $data=json_decode($json);
     $status = $data->status;
     if($status=="OK")
     {
       return $data->results[0]->formatted_address;
     }
     else
     {
       return false;
     }
  }
?>
Run Code Online (Sandbox Code Playgroud)

在getaddress()函数中传递纬度和经度.它会在成功时返回地址字符串,否则返回布尔值false.

<?php
  $lat= 26.754347; //latitude
  $lng= 81.001640; //longitude
  $address= getaddress($lat,$lng);
  if($address)
  {
    echo $address;
  }
  else
  {
    echo "Not found";
  }
?>
Run Code Online (Sandbox Code Playgroud)

  • URL 应该是 https。Google 现在要求对此 API 的请求必须通过 SSL 进行。否则您会收到“REQUEST_DENIED”错误。 (2认同)

小智 6

<?php

/* 
* Given longitude and latitude in North America, return the address using The Google Geocoding API V3
*
*/

function Get_Address_From_Google_Maps($lat, $lon) {

$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lon&sensor=false";

// Make the HTTP request
$data = @file_get_contents($url);
// Parse the json response
$jsondata = json_decode($data,true);

// If the json data is invalid, return empty array
if (!check_status($jsondata))   return array();

$address = array(
    'country' => google_getCountry($jsondata),
    'province' => google_getProvince($jsondata),
    'city' => google_getCity($jsondata),
    'street' => google_getStreet($jsondata),
    'postal_code' => google_getPostalCode($jsondata),
    'country_code' => google_getCountryCode($jsondata),
    'formatted_address' => google_getAddress($jsondata),
);

return $address;
}

/* 
* Check if the json data from Google Geo is valid 
*/

function check_status($jsondata) {
    if ($jsondata["status"] == "OK") return true;
    return false;
}

/*
* Given Google Geocode json, return the value in the specified element of the array
*/

function google_getCountry($jsondata) {
    return Find_Long_Name_Given_Type("country", $jsondata["results"][0]["address_components"]);
}
function google_getProvince($jsondata) {
    return Find_Long_Name_Given_Type("administrative_area_level_1", $jsondata["results"][0]["address_components"], true);
}
function google_getCity($jsondata) {
    return Find_Long_Name_Given_Type("locality", $jsondata["results"][0]["address_components"]);
}
function google_getStreet($jsondata) {
    return Find_Long_Name_Given_Type("street_number", $jsondata["results"][0]["address_components"]) . ' ' . Find_Long_Name_Given_Type("route", $jsondata["results"][0]["address_components"]);
}
function google_getPostalCode($jsondata) {
    return Find_Long_Name_Given_Type("postal_code", $jsondata["results"][0]["address_components"]);
}
function google_getCountryCode($jsondata) {
    return Find_Long_Name_Given_Type("country", $jsondata["results"][0]["address_components"], true);
}
function google_getAddress($jsondata) {
    return $jsondata["results"][0]["formatted_address"];
}

/*
* Searching in Google Geo json, return the long name given the type. 
* (If short_name is true, return short name)
*/

function Find_Long_Name_Given_Type($type, $array, $short_name = false) {
    foreach( $array as $value) {
        if (in_array($type, $value["types"])) {
            if ($short_name)    
                return $value["short_name"];
            return $value["long_name"];
        }
    }
}

/*
*  Print an array
*/

function d($a) {
    echo "<pre>";
    print_r($a);
    echo "</pre>";
}
Run Code Online (Sandbox Code Playgroud)

请随意查看我的博客,了解如何使用上面的代码和示例结果的示例代码.


dec*_*eze 5

您正在寻找Google(或其他任何人)的反向地理编码服务