2012-09-06 67 views
0

我使用此代碼來獲得用戶的詳細地址信息用戶城市和國家信息獲得使用谷歌GeoApi

function getGeo() { 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function (a) { 
      $("#geoLoc").html("Determing your location.."); 
      $.post("https://mysite.com/getloc.php?l=" + a.coords.latitude + "," + a.coords.longitude, function (b) { 
       var c = jsonParse(b); 
       geo_adres = c.results[0].formatted_address; 
       latitude = a.coords.latitude; 
       longitude = a.coords.longitude; 
       $("#geoLoc").html(c.results[0].formatted_address); 
       $("#geoLoc").show("slow") 
      }) 
     }, function (a) { 
      alert(geolocationErrorMessages[a.code]) 
     }, { 
      enableHighAccuracy: true, 
      maximumAge: 12e4 
     }); 
     return false 
    } else { 
     alert("Your browser doesn't support geo-location feature...") 
    } 
} 

編輯: getloc.php包含此代碼(在C寫的JavaScript VAR)

$data = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng=". $_GET['l'] ."&sensor=false"); 
print $data; 

其實我要的就是這樣city, country

我應該如何改變這一c.results[0].formatted_address到讓用戶城市和國家信息那麼?

+0

你在c中得到什麼? – swapnesh

+0

我編輯了我的問題,請檢查 –

+1

您應該查看geocoder api的文檔,[這裏](https://developers.google.com/maps/documentation/geocoding/#Results)。我將首先檢查數組'c.results [0] .address_components []'的內容。 – Lee

回答

0

試試這個工作的PHP代碼,我希望這將幫助你很多:),讓海鷗知道任何查詢的情況下 -

<?php 

$data = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false"); 

$jsonnew =json_decode($data,true); 

    echo "<pre>"; 
    print_r($jsonnew['results'][7]['address_components'][2]['long_name']); 

echo "<pre>"; 
    print_r($jsonnew['results'][6]['address_components'][2]['long_name']); 
0

你不應該需要你getloc PHP腳本。 Maps Javascript API包含Geocoder類。