php - Geo Coding Address - 获取某个地址的分区(Google API)

标签 php google-maps-api-3 geolocation geocoding jquery-gmap3

我有一个包含准确地址(街道、编号、城市、地区/区域、国家/地区)的数据库。但是,我想知道如果我们在纽约,是否有办法使用 Google API 获取城市的地区(例如“曼哈顿”)?

所有其他信息我已经在数据库中,所以如果有的话我只需要地区(当然这只会在大城市)...

更新:

我在 http://www.techques.com/question/1-3151450/Google-geolocation-API---Use-longitude-and-latitude-to-get-address 上找到了这个函数并尝试将 formatted_address 更改为 sublocality(甚至其他人如 short_name 等)但它不返回任何内容......任何帮助将不胜感激!谢谢!!

function reverse_geocode($lat, $lon) {
    $url = "http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lon&sensor=false";
    $data = json_decode(file_get_contents($url));
    if (!isset($data->results[0]->formatted_address)){
        return "unknown Place";
    }
    return $data->results[0]->formatted_address;
}

最佳答案

您可以像这样访问子区域:

function reverse_geocode($lat, $lon) {
    $url = "http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lon&sensor=false";
    $data = json_decode(file_get_contents($url));
    if (!isset($data->results[0]->address_components)){
        return "unknown Place";
    }

    if ($data->results[0]->address_components[2]->types[0]=="sublocality") {

        $return_array['type']="sublocality";
        $return_array['sublocality_long_name']=$data->results[0]->address_components[2]->long_name;
        $return_array['sublocality_short_name']=$data->results[0]->address_components[2]->short_name;

        return $return_array;
        }

}

关于php - Geo Coding Address - 获取某个地址的分区(Google API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10199152/

相关文章:

php - 这是什么编码?

php - 当用户输入完整地址并且我将城市存储在我的数据库中时如何仅检查城市名称

Android:使用 LocationManager.requestLocationUpdates() 时如何从 Intent bundle 中获取位置信息

javascript - 地理位置 API - 不同的结果

php - phpMyAdmin 和 CakePHP 3 中的结果不同

php - 扩展 Woocommerce 中自定义元字段的管理产品搜索

javascript - 响应式 Google map API v3 - 自动在小屏幕上显示所有标记

javascript - Google map - 无法读取未定义的属性 '__e3_'

ruby - 在 Ruby 中将 lat lng/zip/city/state 转换为时区的最简单方法

php - 数组到字符串转换 SYmfony Sql