加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 13:39:03 所属栏目:PHP教程 来源:网络整理
导读:我使用了地理编码的自动完成,但是当我从下拉菜单中选择时,它给我的地址和长的地址 我需要检查lat和long是否空,然后从发布的地址我必须得到纬度和经度 假设你有lat和long的隐藏值 mapLat mapLong和输入字段名称是位置 那么你可以检查如下 htmlforminput type=
我使用了地理编码的自动完成,但是当我从下拉菜单中选择时,它给我的地址和长的地址

我需要检查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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读