从地理编码结果中获取城市信息?

45

在处理地理编码结果时,无法获取不同数组的内容。

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

15个回答

0

我使用了一个叫做find的lodash函数,它会返回谓词函数返回true的对象。就这么简单!

let city = find(result, (address) => {
  return typeof find(address.types, (a) => { return a === 'locality'; }) === 'string';
});

0
正如大多数答案所说,地址组件随时变化,并且在不同的国家/地区之间差异巨大。你不能仅仅依靠“locality”来确定,因为在某些地址和地区中可能没有提供该信息。以印度尼西亚为例,查看这个地点的ID:EjdOeXVoIEt1bmluZyBSb2FkLCBNQVMsIEdpYW55YXIgUmVnZW5jeSwgQmFsaSwgSW5kb25lc2lhIi4qLAoUChIJJzq08aA90i0Rb3AEsy1pPUoSFAoSCcsgYMOTPdItEYAPg8r7CwMF 更可靠的方法是使用“administrative_area_level”类型来查找城市。如果没有提供“locality”,那么可以选择较低级别的行政区域(或者你喜欢的其他级别),像这样:
function getAddressComponents(address_components) {

  let address = ''
  let postcode = ''
  let city = ''
  let country = ''
  let country_code = ''
  let postal_code = ''
  let admin_areas: string[] = []

  for (const component of address_components as google.maps.GeocoderAddressComponent[]) {
    const componentType = component.types[0]

    switch (componentType) {
      case 'country':
        country = component.long_name
        country_code = component.short_name
        break
      case 'postal_town':
      case 'locality':
        if (city) {
          city += ', '
        }
        city += component.long_name
        break
      case 'street_number':
      case 'route':
        if (address) {
          address += ', '
        }
        address += component.long_name
        break
      case 'postal_code':
      case 'postal_code_suffix':
        if (postal_code) {
          postal_code += ', '
        }
        postal_code += component.long_name
        break
      default:
        if (componentType.startsWith('administrative_area_level')) {
          admin_areas.push(component.long_name)
        }
    }
  }
  if(!city) {
    city = admin_areas[0]
  }
  return { address, postcode, country, city }
}

0
            //if (arrAddress[ac].types[0] == "street_number") { alert(arrAddress[ac].long_name) } // SOKAK NO
            //if (arrAddress[ac].types[0] == "route") { alert(arrAddress[ac].short_name); } // CADDE
            //if (arrAddress[ac].types[0] == "locality") { alert(arrAddress[ac].long_name) } // İL
            //if (arrAddress[ac].types[0] == "administrative_area_level_1") { alert(arrAddress[ac].short_name) } // İL
            //if (arrAddress[ac].types[0] == "postal_code") { alert(arrAddress[ac].long_name); } // POSTA KODU
            //if (arrAddress[ac].types[0] == "neighborhood") { alert(arrAddress[ac].long_name); } // Mahalle
            //if (arrAddress[ac].types[0] == "sublocality") { alert(arrAddress[ac].long_name); } // İlçe
            //if (arrAddress[ac].types[0] == "country") { alert(arrAddress[ac].long_name); } // Ülke

0

如果你想获取城市,这对我很有效。

var city = "";
function getPositionByLatLang(lat, lng) {
    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]) {
                //formatted address
                city = results[0].plus_code.compound_code;
                city = city.substr(0, city.indexOf(','));
                city = city.split(' ')[1];

                console.log("the name of city is: "+ city);
            }
        } else {
            // alert("Geocoder failed due to: " + status);
        }
    });
}

-1
您可以不通过迭代即可获取城市信息,通常城市信息位于address_components对象的第2个键中,因此第2个意思是1:
results[0].address_components[1].long_name

这不是真的。你怎么得出这个结论的? - Čamo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接