从谷歌地图获取城市州

时间:2009-07-08 15:24:55

标签: php javascript google-maps

我正在使用谷歌地图api来显示位置。在我的情况下,我需要选择从谷歌地图标记获取城市sate zip。在地图中,用户可以在拖动完成后将标记移动到它的位置,它将返回城市,州和拉链。我已经成功获得了lat和lng,但我怎样才能获得城市,州和邮政。请尽快帮助我..

3 个答案:

答案 0 :(得分:4)

Google Maps API使用rather verbose format提取特定地址数据:

geocoder.getLocations(latlng, showAddress);

function showAddress(response){
    if (response && response.Status.code == 200){
        var place = reponse.Placemark[0]
        var city  = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
        var state = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
        var zip   = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
    }
}

答案 1 :(得分:1)

对Javascript方式不太确定,但我使用GWT Google Maps库这样做

geocoder.getLocations(<<Your Address>>, new LocationCallback() {
    public void onFailure(int statusCode) {
       Info.show("Failed","Unable to geocode that address.","");
    }

    public void onSuccess(JsArray<Placemark> locations) {
       final Placemark place = locations.get(0);
       Window.alert(place.getCity() + place.getState() + place.getCountry() + place.getPostalCode());
    } });

答案 2 :(得分:0)

http://code.google.com/apis/maps/documentation/services.html#ReverseGeocoding

geocoder = new GClientGeocoder();
...
geocoder.getLocations(latlng, showAddress);

(showAddress是一个函数)