通过jquery/ajax与google maps geocode从纬度/经度获取城市/州?

Mar*_*ark 7 javascript jquery google-maps google-maps-api-3

我已经看到了这个概念的一些jscript/jquery实现,你可以输入一个邮政编码并从google maps api获得一个long/lat.

但是,在我的情况下,我已经有了一组坐标,并且想知道当我们通过jquery提供长/纬度时,是否可以动态地从API获取文本City,State结果?

谢谢!

kie*_*ran 17

这是一个称为反向地理编码的过程,谷歌有相当广泛的文档.

一个例子是:

$.ajax({ url:'http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true',
         success: function(data){
             alert(data.results[0].formatted_address);
             /*or you could iterate the components for only the city and state*/
         }
});
Run Code Online (Sandbox Code Playgroud)

  • Dude我得到了答案(var i = 0; i <data.results [4] .address_components.length; i ++){for(var j = 0; j <data.results [4] .address_components [i] .types .length; j ++){if(data.results [4] .address_components [i] .types [j] =='country'){var country_code = data.results [4] .address_components [i] .short_name; 警报(COUNTRY_CODE); }}} (2认同)