在我的应用程序中,当您单击 map 上的一个点时,它会返回地址。示例:
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)
}
});
控制台显示:
Object {Address: "1029 W Cermak Rd", Neighborhood: null, City: "Chicago", Subregion: null, Region: "Illinois"…}
有没有办法将地址转换成字符串。基本上我只想: 1029 W Cermak Rd., Chicago, Illinois.
谢谢。
最佳答案
没有通用的方法可以做到这一点。您只需要连接您感兴趣的字段。您可以使用 filter
过滤掉 nil/空字段,然后 join
结果。
var address = evt.address.address;
addressString = [
address.Address,
address.Neighborhood,
address.City,
address.Region,
address.Subregion,
].filter(function (val) { return val }).join(", ");
关于javascript - 将对象地址转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24498602/