谷歌地图 - 将地址转换为纬度和经度 - PHP 后端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18595830/
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
Google Maps - converting address to latitude & longitude - PHP backend?
提问by Wordpressor
is it possible to convert (in PHP back-end):
是否可以转换(在 PHP 后端):
<?php
$Address = "Street 1, City, Country"; // just normal address
?>
to
到
<?php
$LatLng = "10.0,20.0"; // latitude & lonitude
?>
The code I'm currently using is the default one:
我目前使用的代码是默认的:
<script type='text/javascript' src='https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false'></script>
<script>
function initialize() {
var myLatlng = new google.maps.LatLng(10.0,20.0);
var mapOptions = {
zoom: 16,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'This is a caption'
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="map"></div>
I've been reading https://developers.google.com/maps/documentation/geocodingfor a while now but I'm not experienced enough I guess.
我已经阅读https://developers.google.com/maps/documentation/geocoding一段时间了,但我想我的经验还不够。
Thanks.
谢谢。
回答by Neil Robertson
This works for me:
这对我有用:
<?php
$Address = urlencode($Address);
$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$Address."&sensor=true";
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;
if ($status=="OK") {
$Lat = $xml->result->geometry->location->lat;
$Lon = $xml->result->geometry->location->lng;
$LatLng = "$Lat,$Lon";
}
?>
References:
参考:
回答by Moh
<?php
/*
* Given an address, return the longitude and latitude using The Google Geocoding API V3
*
*/
function Get_LatLng_From_Google_Maps($address) {
$address = urlencode($address);
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&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();
$LatLng = array(
'lat' => $jsondata["results"][0]["geometry"]["location"]["lat"],
'lng' => $jsondata["results"][0]["geometry"]["location"]["lng"],
);
return $LatLng;
}
/*
* Check if the json data from Google Geo is valid
*/
function check_status($jsondata) {
if ($jsondata["status"] == "OK") return true;
return false;
}
/*
* Print an array
*/
function d($a) {
echo "<pre>";
print_r($a);
echo "</pre>";
}
For sample code of how using the function above, feel free to visit my blog
有关如何使用上述功能的示例代码,请随时访问我的博客
回答by datasage
Yes you can. You would do a request with file_get_contents
or curl
to a url like this:
是的你可以。您可以使用file_get_contents
或curl
向这样的网址发出请求:
http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false
http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false
What is returned is a json encoded response. You can parse this into arrays using json_decode
.
返回的是一个 json 编码的响应。您可以使用 将其解析为数组json_decode
。
The documentation page has an example of the what the response may look like, use that to find the data that you need.
文档页面有一个响应可能是什么样子的示例,使用它来查找您需要的数据。
回答by Gaurav kumar MVC Expert
this will work please try this one i have tested it .
<?php
$address = 'Street 1, City, Country'; // Your address
$prepAddr = str_replace(' ','+',$address);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
echo $address.'<br>Lat: '.$lat.'<br>Long: '.$long;
?>