Nic*_*Huh 0 javascript google-maps google-maps-api-3
我有这个功能:
function createMarkersForPlaces(places, infowindow) {
for (var i = 0; i < places.length; i++) {
var place = places[i];
var marker = new google.maps.Marker({
map: map,
title: place.name,
position: place.geometry.location,
id: place.place_id,
animation: google.maps.Animation.DROP
});
placeMarkers.push(marker);
// if user clicks one of the marker, execute getPlaceDetails
// for that specific place
marker.addListener('click', function() {
if (infowindow.marker == this) {
} else {
getPlacesDetails(this, infowindow);
}
});
// based on the places above, populate list in html
$("ul").append("<li><a href='#' onclick='getPlacesDetails(" + marker + "," + infowindow ")' class='w3-bar-item'>" + place.name + "</a></li>");
}
}
Run Code Online (Sandbox Code Playgroud)
但这行代码不起作用。
$("ul").append("<li><a href='#' onclick='getPlacesDetails(" + marker + "," + infowindow ")' class='w3-bar-item'>" + place.name + "</a></li>");
Run Code Online (Sandbox Code Playgroud)
在定义了标记和信息窗口的函数内部,除了这行代码之外,该函数运行完美。Marker 是 google.maps.marker 中的对象,infowindow 是 google.maps.InfoWindow 中的对象。我怎样才能让它发挥作用?
function getPlacesDetails(marker, infowindow) {some function...}
Run Code Online (Sandbox Code Playgroud)
marker
是一个对象,因此您无法将其转换为要放入onclick
属性中的字符串,并且它位于窗口范围之外,因此无论如何您都无法直接在onclick
属性中执行此操作。我可能会这样做:
var $li = $("<li><a href='#' class='w3-bar-item'>" + place.name + "</a></li>");
$("ul").append($li);
$li.on('click', getPlacesDetails.bind(this, marker, infowindow));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10968 次 |
最近记录: |