简体   繁体   English

将对象地址转换为字符串

[英]Convert object address to string

In my application when you single-click a point on the map it returns the address. 在我的应用程序中,当您单击地图上的一个点时,它将返回地址。 Example: 例:

locator.on("location-to-address-complete", function(evt) {
if (evt.address.address) {
      var address = evt.address.address;
      var location = webMercatorUtils.geographicToWebMercator(evt.address.location);
      console.log(address)
    }
});

console displays: 控制台显示:

Object {Address: "1029 W Cermak Rd", Neighborhood: null, City: "Chicago", Subregion: null, Region: "Illinois"…}

Is there a way to convert the address to a string. 有没有一种方法可以将地址转换为字符串。 Basically I just want: 1029 W Cermak Rd., Chicago, Illinois. 基本上我只想知道:伊利诺伊州芝加哥市Cermak Rd 1029W。

Thank you. 谢谢。

There is no generic way to do this. 没有通用的方法可以做到这一点。 You just need to concatenate the fields you're interested in. You can use filter to filter out the nil/empty fields, and then join the results. 您只需要串联所需的字段即可。可以使用filter过滤掉nil / empty字段,然后join结果。

var address = evt.address.address;

addressString = [
  address.Address,
  address.Neighborhood,
  address.City,
  address.Region,
  address.Subregion,
].filter(function (val) { return val }).join(", ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM