7

正確な住所 (番地、番地、都市、地域/エリア、国) のデータベースがあります。しかし、ニューヨークにいる場合、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;
}
4

3 に答える 3

7

次のようにサブローカリティにアクセスできます。

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

}
于 2012-04-18T19:42:15.987 に答える
3

typesに設定された結果が存在する場合、この情報は geocode-request 内で見つかります。

 [ "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
于 2012-04-17T23:04:31.217 に答える