1

現在、グーグルマップAPIで作業しています。グーグルマップAPIでは、特定の住所から経度と緯度を取得しています。

コードは次のとおりです。

$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address=1746+Esterbrook+Dr&sensor=false');

$output= json_decode($geocode);

$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng; 

この緯度と経度では、ライブサーバーでは値がempty(null)として表示されます。ローカルホストマシンでは、完全に正常に機能しています。グーグルマップを表示するために含めるものはありますか?

4

1 に答える 1

1

個人的には、あいまいさが少ない配列を返す方が好きです。これを行うには、jsonデコードで2番目のパラメーターをtrueに設定します。また、常に結果が返されるとは限らないため、ifステートメントを追加したことをお勧めします。このURLに従ってください。

PHP.net関数json-デコード

$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address=1746+Esterbrook+Dr&sensor=false');


if(strlen($geocode)>0){

  $output= json_decode($geocode, true);

  if(is_array($output) && count($output)>0)
  {
    $output[0]['location']['lat']  ;
    print_r($output); // the printr will give you a clue for the exact nesting to use above
  }

}
于 2013-03-01T16:48:00.060 に答える