在我的应用程序中,当您单击地图上的某个点时,它将返回该地址。例如:
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。
谢谢。
答案 0 :(得分:4)
没有通用的方法来做到这一点。您只需要连接您感兴趣的字段。您可以使用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(", ");