【问题标题】:Geo Coding Address - get district of a certain address (Google API)地理编码地址 - 获取某个地址的地区(Google API)
【发布时间】:2012-04-29 06:14:43
【问题描述】:

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

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

更新:

我在http://www.techques.com/question/1-3151450/Google-geolocation-API---Use-longitude-and-latitude-to-get-address 上找到了这个函数,并尝试将formatted_address 更改为子本地(甚至是其他类似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;
}

【问题讨论】:

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


    【解决方案1】:

    我想出了以下内容。

    function geocode() {
        var geocoder = new google.maps.Geocoder();
        var lat  = $('#latitude').val()
        var lng  = $('#longitude').val()
        var latlng = {lat: parseFloat(lat), lng: parseFloat(lng)};
    
      geocoder.geocode(
        {'location': latlng},
        function(results, status) {
          if (status === 'OK') {
            for (var i = 0; i < results[0].address_components.length; i++)
            {
              if (status == google.maps.GeocoderStatus.OK) {
        				if (results[0]) {
        					for (var i = 0; i < results.length; i++) {
                    //alert(results[i].types[0]+','+results[i].types[1]+','+results[i].address_components[0].long_name)
                    //district
                    if (results[i].types[0]=='political' && results[i].types[1]=='sublocality' ){
                      alert(results[i].address_components[0].long_name);
                    }
                    //City
                    if (results[i].types[0]=='locality' && results[i].types[1]=='political' ){
                      alert(results[i].address_components[0].long_name);
                    }
                    //country
                    if (results[i].types[0]=='country' && results[i].types[1]=='political' ){
                      alert(results[i].address_components[0].long_name);
                    }
        					}
        				}
        				else {console.log("No reverse geocode results.")}
        			}
        			else {console.log("Geocoder failed: " + status)}
    
    
            }
      }})
    }
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

    【讨论】:

      【解决方案2】:

      当存在将types 设置为的结果时,您将在地理编码请求中找到此信息

       [ "sublocality", "political" ]
      

      示例:317 Madison Ave,New York City


      修改上面的函数以便于访问响应组件:

        /**
          * @param $a mixed latitude or address
          * @param $b mixed optional longitude when $a is latitude
          * @return object geocoding-data
          **/
      
          function geocode($a, $b=null) {
          $params=array('sensor'=>'false');
          if(is_null($b))
          {
            $params['address']=$a;
          }
          else
          {
            $params['latlng']=implode(',',array($a,$b));
          }
          $url = 'http://maps.google.com/maps/api/geocode/json?'.http_build_query($params,'','&');
          $result=@file_get_contents($url);
      
           $response=new StdClass;
           $response->street_address               = null;
           $response->route                        = null;
           $response->country                     = null;
           $response->administrative_area_level_1 = null;
           $response->administrative_area_level_2 = null;
           $response->administrative_area_level_3 = null;
           $response->locality                    = null;
           $response->sublocality                 = null;
           $response->neighborhood                = null;
           $response->postal_code                 = null;
           $response->formatted_address           = null;
           $response->latitude                    = null;
           $response->longitude                   = null;
           $response->status                      = 'ERROR';
      
          if($result)
          {
            $json=json_decode($result);
            $response->status=$json->status;
            if($response->status=='OK')
            {
              $response->formatted_address=$json->results[0]->formatted_address;
              $response->latitude=$json->results[0]->geometry->location->lat;
              $response->longitude=$json->results[0]->geometry->location->lng;
      
              foreach($json->results[0]->address_components as $value)
              {
                if(array_key_exists($value->types[0],$response))
                {
                  $response->{$value->types[0]}=$value->long_name;
                }
              }
            }
          }
        return $response;
      }
      
      //sample usage
      echo '<hr/>'.geocode('317 Madison Ave,New York City')->sublocality;
        //Manhattan
      
      echo '<hr/>'.geocode('foobar')->status;
        //ZERO_RESULTS
      
      echo '<hr/>'.geocode('40.689758, -74.04513800000001')->formatted_address;
        //1 Liberty Is, Brooklyn, NY 11231, USA
      

      【讨论】:

      • 感谢 Molle 博士的分析和帮助!我找到了这个函数(请参阅我的帖子更新),但如果我只是将 formatted_address 更改为其他任何内容,它会返回“未知地址”......您知道如何仅使用此函数获取子位置吗?
      • 添加了修改后的函数,方便访问响应的单个组件。
      【解决方案3】:

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

      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;
              }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2013-10-05
        • 1970-01-01
        • 2016-07-20
        • 2016-03-04
        • 1970-01-01
        • 2015-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多