<分区>
我使用了地理编码的自动完成功能,但是当我从下拉列表中选择时,它会给我地址的纬度和经度
我需要检查纬度和经度何时为空然后从发布的地址我必须得到纬度和经度
<分区>
我使用了地理编码的自动完成功能,但是当我从下拉列表中选择时,它会给我地址的纬度和经度
我需要检查纬度和经度何时为空然后从发布的地址我必须得到纬度和经度
最佳答案
假设您有经纬度的隐藏值是 mapLat & mapLong 和输入字段名称是位置 然后:
<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>
extract($_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 - 从地址获取纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23212681/