要从地理编码器结果中获取城市?

问题描述 投票:43回答:12

从地址解析器结果获取不同数组内容时遇到问题。

item.formatted_address有效,但item.address_components.locality不起作用?

geocoder.geocode( {'address': request.term }, function(results, status) {

        response($.map(results, function(item) {

        alert(item.formatted_address+" "+item.address_components.locality)
    }            
}); 

//返回的数组是;

 "results" : [
      {
         "address_components" : [
            {
               "long_name" : "London",
               "short_name" : "London",
               "types" : [ "locality", "political" ]
            } ],
          "formatted_address" : "Westminster, London, UK" // rest of array...

任何帮助表示赞赏!

Dc

google-maps geocoding street-address city
12个回答
57
投票

最后使用:

var arrAddress = item.address_components;
var itemRoute='';
var itemLocality='';
var itemCountry='';
var itemPc='';
var itemSnumber='';

// iterate through address_component array
$.each(arrAddress, function (i, address_component) {
    console.log('address_component:'+i);

    if (address_component.types[0] == "route"){
        console.log(i+": route:"+address_component.long_name);
        itemRoute = address_component.long_name;
    }

    if (address_component.types[0] == "locality"){
        console.log("town:"+address_component.long_name);
        itemLocality = address_component.long_name;
    }

    if (address_component.types[0] == "country"){ 
        console.log("country:"+address_component.long_name); 
        itemCountry = address_component.long_name;
    }

    if (address_component.types[0] == "postal_code_prefix"){ 
        console.log("pc:"+address_component.long_name);  
        itemPc = address_component.long_name;
    }

    if (address_component.types[0] == "street_number"){ 
        console.log("street_number:"+address_component.long_name);  
        itemSnumber = address_component.long_name;
    }
    //return false; // break the loop   
});

0
投票

我使用了一个名为find的lodash函数,该函数返回谓词为其返回true的对象。就这么简单!


0
投票

返回位置(如果存在)。如果不是,则返回let city = find(result, (address) => { return typeof find(address.types, (a) => { return a === 'locality'; }) === 'string'; });


0
投票

我创建此函数来获取地址解析器结果的主要信息:


15
投票

尝试了几个不同的请求:


10
投票

我必须创建一个程序,当用户单击地图上的位置时,该程序将在用户表单中填写纬度,经度,城市,县和州字段。该页面可以在http://krcproject.groups.et.byu.net处找到,它是一种用户表单,允许公众对数据库做出贡献。我并没有声称自己是专家,但是效果很好。


6
投票

我假设您想获得城市和州/省:


3
投票

我认为,谷歌没有提供某种功能来获取这些功能真是太痛苦了。无论如何,我认为找到正确对象的最佳方法是:


3
投票

这对我有用:


1
投票
Array.prototype.includes

0
投票
// 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;
    })
}

0
投票

以下是您可以与lodash js库一起使用的一些代码:(只需将$ scope.x替换为您自己的变量名以存储值)

© www.soinside.com 2019 - 2024. All rights reserved.