2012-04-03 17 views
9

GoogleマップAPI(V3?) - PHPを使用して経度と緯度から国、地域、都市名を取得したいと考えています。私はちょうど各郡のためにそれを得ることができません、私はこのコードを取得することができましたが、返された結果が異なるため、他の郡では機能しません(唯一の正しい情報は国ですが、英語名):Googleマップ - 経度と緯度から国、地域、都市名を取得する(PHP)

<? 
    $lok1 = $_REQUEST['lok1']; 
    $lok2 = $_REQUEST['lok2']; 

    $url = 'http://maps.google.com/maps/geo?q='.$lok2.','.$lok1.'&output=json'; 
    $data = @file_get_contents($url); 
    $jsondata = json_decode($data,true); 

    if(is_array($jsondata)&& $jsondata ['Status']['code']==200) 
    { 
      $addr = $jsondata ['Placemark'][0]['AddressDetails']['Country']['CountryName']; 
      $addr2 = $jsondata ['Placemark'][0]['AddressDetails']['Country']['Locality']['LocalityName']; 
      $addr3 = $jsondata ['Placemark'][0]['AddressDetails']['Country']['Locality']['DependentLocality']['DependentLocalityName']; 
    } 
    echo "Country: " . $addr . " | Region: " . $addr2 . " | City: " . $addr3; 
?> 

これは私の国からではなく、他人のために正しいデータを取得するために素晴らしい作品...

+0

あなたにはうってつけのコーディネートはありませんか? – cloakedninjas

+0

それは私のために完全に動作します。しかし私のアプリケーションはインドでのみ使用しています。 – srbhbarot

+0

あなたが試した座標について詳しく述べるべきです。 – srbhbarot

答えて

23

あなたはこのように(トーマスによって提案された)第2のAPIを使用することができます。

$geocode=file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=48.283273,14.295041&sensor=false'); 

     $output= json_decode($geocode); 

echo $output->results[0]->formatted_address; 

はこれを試してみてください。..

編集:::::

$geocode=file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=48.283273,14.295041&sensor=false'); 

     $output= json_decode($geocode); 

    for($j=0;$j<count($output->results[0]->address_components);$j++){ 
       echo '<b>'.$output->results[0]->address_components[$j]->types[0].': </b> '.$output->results[0]->address_components[$j]->long_name.'<br/>'; 
      } 
+0

これは動作しますが、データは必要なものではありません。まあ、ここからは国と地域を取得できますが(エリアにはポスト番号も含まれます)、都市専用アドレスは取得できません。どのように私は国、都市/村の名前と地域(郵便番号なし)を取得することができます。 –

+0

私の編集した答えを確認してください – srbhbarot

+0

これは、ありがとうございます。正解と表示されます。ありがとうございました。 –

1

は、あなたが彼らのジオコーディングAPIを使用してみましたか?

https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding

例: http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false

編集:私はインドをターゲットに、多くのアプリケーションでそれを使用しているし、それはかなりうまくいきました。

+0

PHPで国、都市、地域を手伝ってもらえますか? –

5
<?php 
$deal_lat=30.469301; 
$deal_long=70.969324; 
$geocode=file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$deal_lat.','.$deal_long.'&sensor=false'); 

     $output= json_decode($geocode); 

    for($j=0;$j<count($output->results[0]->address_components);$j++){ 
       $cn=array($output->results[0]->address_components[$j]->types[0]); 
      if(in_array("country", $cn)) 
      { 
      $country= $output->results[0]->address_components[$j]->long_name; 
      } 
      } 
      echo $country; 
?> 
+0

都市名を探して、郵便番号を探してからreplacein_array( "country"、$ cn)を検索する必要がある場合は、in_array( "country"、$ cn)をin_array( "locality"、$ cn) to_array( "postal_code"、$ cn) –

6

がここにあります問題への私のアプローチ

$lat = "22.719569"; 
$lng = "75.857726"; 
$data = file_get_contents("http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lng&sensor=false"); 
$data = json_decode($data); 
$add_array = $data->results; 
$add_array = $add_array[0]; 
$add_array = $add_array->address_components; 
$country = "Not found"; 
$state = "Not found"; 
$city = "Not found"; 
foreach ($add_array as $key) { 
    if($key->types[0] == 'administrative_area_level_2') 
    { 
    $city = $key->long_name; 
    } 
    if($key->types[0] == 'administrative_area_level_1') 
    { 
    $state = $key->long_name; 
    } 
    if($key->types[0] == 'country') 
    { 
    $country = $key->long_name; 
    } 
} 
echo "Country : ".$country." ,State : ".$state." ,City : ".$city; 
関連する問題