PHP 纬度经度地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12810654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Latitude Longitude to Address
提问by Joe Torraca
I have a form on my website where a user enters an address of a place. When they submit the form, I convert this location into latitude/longitude and store this in a MySQL Database. I am using Google's Geocode service for this conversion. The problem is that I can't find either a class or a service to convert that latitude/longitude back to an address and as far as I know, Google's Geocode service is a unidirectional conversion. I realize I could store the physical address in the database, but over time this is wasted space when it could be stored in a simpler format. Does anybody know of either a class/service to convert from latitude/longitude to a address or if I am wrong and I can use Google's geocode system? I have looked for the answer for the past few days but couldn't find anything. Thanks for your help!
我的网站上有一个表格,用户可以在其中输入一个地点的地址。当他们提交表单时,我将此位置转换为纬度/经度并将其存储在 MySQL 数据库中。我正在使用 Google 的地理编码服务进行此转换。问题是我找不到将纬度/经度转换回地址的类或服务,据我所知,Google 的地理编码服务是单向转换。我意识到我可以将物理地址存储在数据库中,但随着时间的推移,当它可以以更简单的格式存储时,这会浪费空间。有没有人知道从纬度/经度转换为地址的类/服务,或者如果我错了,我可以使用谷歌的地理编码系统?过去几天我一直在寻找答案,但找不到任何东西。谢谢你的帮助!
回答by Andre Hoffmann
Conversion of geo-coordinates into address is known as Reverse Geocoding. In this script we are using Google map API because it's free, fast and no API key required.
地理坐标到地址的转换称为反向地理编码。在此脚本中,我们使用 Google 地图 API,因为它免费、快速且不需要 API 密钥。
Rate Limit of Google reveres Geocoding is 2500 API calls per IP per day.
Google 尊崇地理编码的速率限制是每个 IP 每天 2500 个 API 调用。
PHP function for Reveres Geocoding
用于 Reveres 地理编码的 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;
}
}
?>
Pass latitude and longitude in getaddress() function. It will return address string on success otherwise return boolean false.
在 getaddress() 函数中传递纬度和经度。成功时将返回地址字符串,否则返回布尔值 false。
Example
例子
<?php
$lat= 26.754347; //latitude
$lng= 81.001640; //longitude
$address= getaddress($lat,$lng);
if($address)
{
echo $address;
}
else
{
echo "Not found";
}
?>
回答by Moh
<?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>";
}
回答by deceze
You're looking for Google's (or anyone else's) reverse geocodingservice.
您正在寻找 Google 的(或其他任何人的)反向地理编码服务。
回答by vishnu sharma
Geolocation PHP convert Latitude Longitude to Address. First you need to get the api key for google map api that you find using below link:
地理定位 PHP 将纬度经度转换为地址。首先,您需要使用以下链接获取 google map api 的 api 密钥:
https://developers.google.com/maps/documentation/geocoding/start#ReverseGeocoding
https://developers.google.com/maps/documentation/geocoding/start#ReverseGeocoding
After put the below function into your helper class and call that function anywhere you want simple passing lat and long. After passing lat long they return address regarding lat long values. That whole process called Reverse Geocoding.
将以下函数放入您的助手类后,并在您想要简单传递经纬度的任何地方调用该函数。通过 lat long 后,它们返回有关 lat long 值的地址。整个过程称为反向地理编码。
/**
* find address using lat long
*/
public static function geolocationaddress($lat, $long)
{
$geocode = "https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&sensor=false&key=AIzaSyCJyDp4TLGUigRfo4YN46dXcWOPRqLD0gQ";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $geocode);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$output = json_decode($response);
$dataarray = get_object_vars($output);
if ($dataarray['status'] != 'ZERO_RESULTS' && $dataarray['status'] != 'INVALID_REQUEST') {
if (isset($dataarray['results'][0]->formatted_address)) {
$address = $dataarray['results'][0]->formatted_address;
} else {
$address = 'Not Found';
}
} else {
$address = 'Not Found';
}
return $address;
}
For more details checkout below link: Geolocation PHP Latitude Longitude to Address - Lelocode
有关更多详细信息,请查看以下链接: 地理定位 PHP 纬度经度到地址 - Lelocode

