get city from geocoder results?

后端 未结 13 1952
我寻月下人不归
我寻月下人不归 2020-12-02 14:07

Having problems getting the different arrays content from geocoder results.

item.formatted_address works but not item.address_components.locality?

ge         


        
13条回答
  •  心在旅途
    2020-12-02 14:59

    // Use Google Geocoder to get Lat/Lon for Address
    function codeAddress() {
        // Function geocodes address1 in the Edit Panel and fills in lat and lon
        address = document.getElementById("tbAddress").value;
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                loc[0] = results[0].geometry.location.lat();
                loc[1] = results[0].geometry.location.lng();
                document.getElementById("tbLat").value = loc[0];
                document.getElementById("tbLon").value = loc[1];
                var arrAddress = results[0].address_components;
                for (ac = 0; ac < arrAddress.length; ac++) {
                    if (arrAddress[ac].types[0] == "street_number") { document.getElementById("tbUnit").value = arrAddress[ac].long_name }
                    if (arrAddress[ac].types[0] == "route") { document.getElementById("tbStreet").value = arrAddress[ac].short_name }
                    if (arrAddress[ac].types[0] == "locality") { document.getElementById("tbCity").value = arrAddress[ac].long_name }
                    if (arrAddress[ac].types[0] == "administrative_area_level_1") { document.getElementById("tbState").value = arrAddress[ac].short_name }
                    if (arrAddress[ac].types[0] == "postal_code") { document.getElementById("tbZip").value = arrAddress[ac].long_name }
                }
                document.getElementById("tbAddress").value = results[0].formatted_address;
            }
            document.getElementById("pResult").innerHTML = 'GeoCode Status:' + status;
        })
    }
    

提交回复
热议问题