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

标签 php google-maps google-geocoder

<分区>

我使用了地理编码的自动完成功能,但是当我从下拉列表中选择时,它会给我地址的纬度和经度

我需要检查纬度和经度何时为空然后从发布的地址我必须得到纬度和经度

最佳答案

假设您有经纬度的隐藏值是 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&region=$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/

相关文章:

php - mysql无法创建外键错误121但不存在

php - 为什么我的数据库表在此查询后没有更新?

php - 放入 session 后,Laravel session key 为空

android - MapView保持上下文导致内存泄漏

google-maps - 可以调整亮度或在 Leaflet Map 上应用模糊效果吗?

perl - 使用 perl 和 Google 的 Geocode API 获取地址的纬度和经度

php - session_start() php 中的 UnexpectedValueException 导致 SPLObjectStorage 序列化失败

jquery - Google 按类名自动完成位置不起作用

google-places-api - 从给定的城市谷歌地点 API 检索所有地点和子地点

android - 适用于 Android 的 Google Places 自动完成仅捕获街道