2012-09-06 107 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类。