簡體   English   中英

在Google地圖中獲取具有經度和緯度的地址和郵政編碼

[英]get address and zipcode with longitude and latitude in google map

在谷歌地圖API中,我想提取地圖中心的緯度和經度,並像郵政編碼一樣獲取那里的地址。 這有可能獲得郵政編碼嗎?
我為此目的使用它

var lat = -24.448674;
        var lng = 135.684569;
        var geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(lat, lng);
        geocoder.geocode({'latLng': latlng}, function(results, status)
        {
            if (status == google.maps.GeocoderStatus.OK)
            {
                if (results[1]) {
                    alert(results[1].formatted_address);
                } else {
                    alert("No results found");
                }
            }
            else
            {
                alert("Geocoder failed due to: " + status);
            }
        });

但是在上面的代碼中,我無法獲取郵政編碼和郵政編碼!
https://developers.google.com/maps/documentation/geocoding/

你可以得到郵政編碼搜索postal_code類型:

    if (status == google.maps.GeocoderStatus.OK) {
        if (results[0]) {
            for (var i = 0; i < results[0].address_components.length; i++) {
                var types = results[0].address_components[i].types;

                for (var typeIdx = 0; typeIdx < types.length; typeIdx++) {
                    if (types[typeIdx] == 'postal_code') {
                        //console.log(results[0].address_components[i].long_name);
                        console.log(results[0].address_components[i].short_name);
                    }
                }
            }
        } else {
            console.log("No results found");
        }
    }

據我所知,您已經在這里回答了自己的問題。 “郵政編碼”是每個國家/地區使用的郵寄地址標識符的通用名稱。 在美國,這是“郵政編碼”,在英國是“郵政編碼”等。

從您鏈接的文檔中:

postal_code表示用於處理該國家/地區內的郵政郵件的郵政編碼。

郵政編碼下的字段可能是您要查找的內容,不要讓命名約定欺騙您。 Google不必為每個國家/地區的可能變化而對字段進行重命名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM