单击标记时在信息窗口内显示数组中的内容
Showing content from an Array inside infowindow on marker click
我有一个 api 端点,它 return 是一个地址数组和地址的一些价格信息,但它没有 return lat/lang,所以我我正在使用地理编码来显示地图上地址的标记,并在每次单击标记时显示信息窗口。现在我希望 infowindow 的内容是来自端点的数组 returned 中地址的价格。希望到目前为止还清楚。现在的问题是每次我单击标记时,信息窗口只显示每个信息窗口上数组的最后(相同)价格,而不显示与该地址相关的价格。请在下面查看我的代码(或检查 fiddlejs => https://jsfiddle.net/puLxak1k/7/)。可能是我试图在异步回调中调用同步,所以我尝试使用 $q.promise 并解析初始 api 调用并尝试在 .then 中调用 geocode.geocoder,但还没有成功。 .
<div id="map-canvas"></div>
function initialize() {
var chicago = new google.maps.LatLng(41.850033, -87.6500523);
var mapOptions = {
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: chicago
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
geocoder = new google.maps.Geocoder()
var someArray = [{price: 10, address: 'cv64ad'}, {price: 20, address: 'cv59bp'}]
for (var i = 0; i < someArray.length; i++) {
var home = someArray[i]
console.log(home.address)
geocoder.geocode({'address': home.address}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
map.setZoom(12);
for (var i in results) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(results[i].geometry.location.A, results[i].geometry.location.F),
map: map
})
google.maps.event.addListener(marker, 'click', someCallback(home))
infowindow = new google.maps.InfoWindow();
function someCallback(home) {
return function (e) {
if (infowindow != null) {
infowindow.close();
}
infowindow.setContent('<div class="infoheading">' + home.price + '</div>');
infowindow.open(map, this);
}
}
}
}
})
}
}
google.maps.event.addDomListener(window, 'load', initialize);
像这样:
https://jsfiddle.net/jvxppeap/2/
诀窍是:创建一个包含标记对象的数组。
然后,在 addListener(marker, 'click' ... 中检查点击了哪个标记;查看我的代码中的 "indexOf" 。
使用该索引,您可以读取 someArray 的正确项目,并且可以在其中添加任何您想要的信息。
var markers = [];
var map;
var infowindow;
function initialize() {
var chicago = new google.maps.LatLng(41.850033, -87.6500523);
var mapOptions = {
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: chicago
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
geocoder = new google.maps.Geocoder();
var someArray = [{price: 10, address: 'cv64ad'}, {price: 20, address: 'cv59bp'}];
for (var i = 0; i < someArray.length; i++) {
var home = someArray[i];
console.log(home.address);
geocoder.geocode({'address': home.address}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
map.setZoom(12);
for (var i in results) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(results[i].geometry.location.A, results[i].geometry.location.F),
map: map
});
// push the marker on the markers array
markers.push(marker);
google.maps.event.addListener(marker, 'click', function (home) {
if (infowindow != null) {
infowindow.close();
}
// now, we want to know which of the markers we're talking about
var index = markers.indexOf(this);
infowindow.setContent('<div class="infoheading">' + someArray[index].price + '</div>');
infowindow.open(map, this);
});
infowindow = new google.maps.InfoWindow();
}
}
})
}
}
google.maps.event.addDomListener(window, 'load', initialize);
我有一个 api 端点,它 return 是一个地址数组和地址的一些价格信息,但它没有 return lat/lang,所以我我正在使用地理编码来显示地图上地址的标记,并在每次单击标记时显示信息窗口。现在我希望 infowindow 的内容是来自端点的数组 returned 中地址的价格。希望到目前为止还清楚。现在的问题是每次我单击标记时,信息窗口只显示每个信息窗口上数组的最后(相同)价格,而不显示与该地址相关的价格。请在下面查看我的代码(或检查 fiddlejs => https://jsfiddle.net/puLxak1k/7/)。可能是我试图在异步回调中调用同步,所以我尝试使用 $q.promise 并解析初始 api 调用并尝试在 .then 中调用 geocode.geocoder,但还没有成功。 .
<div id="map-canvas"></div>
function initialize() {
var chicago = new google.maps.LatLng(41.850033, -87.6500523);
var mapOptions = {
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: chicago
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
geocoder = new google.maps.Geocoder()
var someArray = [{price: 10, address: 'cv64ad'}, {price: 20, address: 'cv59bp'}]
for (var i = 0; i < someArray.length; i++) {
var home = someArray[i]
console.log(home.address)
geocoder.geocode({'address': home.address}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
map.setZoom(12);
for (var i in results) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(results[i].geometry.location.A, results[i].geometry.location.F),
map: map
})
google.maps.event.addListener(marker, 'click', someCallback(home))
infowindow = new google.maps.InfoWindow();
function someCallback(home) {
return function (e) {
if (infowindow != null) {
infowindow.close();
}
infowindow.setContent('<div class="infoheading">' + home.price + '</div>');
infowindow.open(map, this);
}
}
}
}
})
}
}
google.maps.event.addDomListener(window, 'load', initialize);
像这样: https://jsfiddle.net/jvxppeap/2/
诀窍是:创建一个包含标记对象的数组。 然后,在 addListener(marker, 'click' ... 中检查点击了哪个标记;查看我的代码中的 "indexOf" 。 使用该索引,您可以读取 someArray 的正确项目,并且可以在其中添加任何您想要的信息。
var markers = [];
var map;
var infowindow;
function initialize() {
var chicago = new google.maps.LatLng(41.850033, -87.6500523);
var mapOptions = {
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: chicago
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
geocoder = new google.maps.Geocoder();
var someArray = [{price: 10, address: 'cv64ad'}, {price: 20, address: 'cv59bp'}];
for (var i = 0; i < someArray.length; i++) {
var home = someArray[i];
console.log(home.address);
geocoder.geocode({'address': home.address}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
map.setZoom(12);
for (var i in results) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(results[i].geometry.location.A, results[i].geometry.location.F),
map: map
});
// push the marker on the markers array
markers.push(marker);
google.maps.event.addListener(marker, 'click', function (home) {
if (infowindow != null) {
infowindow.close();
}
// now, we want to know which of the markers we're talking about
var index = markers.indexOf(this);
infowindow.setContent('<div class="infoheading">' + someArray[index].price + '</div>');
infowindow.open(map, this);
});
infowindow = new google.maps.InfoWindow();
}
}
})
}
}
google.maps.event.addDomListener(window, 'load', initialize);