PHP - 获取谷歌纵横&地址的经度 [英] PHP - Get Latitude & Longitude from an address
本文介绍了PHP - 获取谷歌纵横&地址的经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用了地理编码的自动完成功能,但是当我从下拉列表中选择时,它会给出地址的经纬度。
I have used the autocomplete of geocode but when I am selecting from the drop down it is giving me the lat and long of the address
我需要检查纬度和长度都是空的,然后从发布的地址我必须得到经度和纬度
I need to check when the lat and long are empty then from the posted address I must get the latitude and longitude
推荐答案
假设你对拉特隐藏值而长的是
mapLat& mapLong和输入字段名称是位置
,那么您可以检查如下
suppose you have hidden value for lat and long is mapLat & mapLong and input field name is location then you can check as below
<html>
<form>
<input type="hidden" name="mapLat">
<input type="hidden" name="mapLong">
<input type="text" name="location">
<input type="submit" name="submit" value="submit">
</form>
</html>
extact($_POST);
if($mapLat =='' && $mapLong ==''){
// Get lat long from google
$latlong = get_lat_long($location); // create a function with the name "get_lat_long" given as below
$map = explode(',' ,$latlong);
$mapLat = $map[0];
$mapLong = $map[1];
}
// function to get the address
function get_lat_long($address){
$address = str_replace(" ", "+", $address);
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$region");
$json = json_decode($json);
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
return $lat.','.$long;
}
这篇关于PHP - 获取谷歌纵横&地址的经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文