PHP – 从地址获取纬度和经度

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP – 从地址获取纬度和经度脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用了地理编码的自动完成,但是当我从下拉菜单中选择时,它给我的地址和长的地址

我需要检查lat和long是否空,然后从发布的地址我必须得到纬度和经度

假设你有lat和long的隐藏值
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>



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&region=$region");
    $json = json_decode($json);

    $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
    $long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
    return $lat.','.$long;
}

脚本宝典总结

以上是脚本宝典为你收集整理的PHP – 从地址获取纬度和经度全部内容,希望文章能够帮你解决PHP – 从地址获取纬度和经度所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。