如何将InfoBoxes与googlemaps实用程序库v3绑定到循环中的点击侦听器
How to tie InfoBoxes to click listeners in for loop with google maps utility library v3?
我正在使用Google Maps Utility Library v3在地图上放置多个可点击的标记。单击某个Marker
时,应打开该特定Marker
唯一的InfoBox
。
我能够在映射上使用每个标记上的'click'
事件侦听器来填充标记,但我很难将每个特定的InfoBox
绑定到其各自的Marker
。我单击的每个图标都会打开我在循环中创建的最后一个InfoBox
。
这是我的代码:
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerwithlabel/src/markerwithlabel.js"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/infobox.js"></script>
<script>
function initialize() {
var crimes = <?php echo json_encode($crimes); ?>;
var count = crimes.length;
var myCoordinates = new google.maps.LatLng( 40.7127837, -74.00594130000002 );
var myOptions = {
zoom: 14,
center: myCoordinates,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var marker, infobox;
var map = new google.maps.Map( document.getElementById( 'map-canvas' ), myOptions );
for(x = 0; x < count; x++)
{
var coordinates = new google.maps.LatLng( parseFloat(crimes[x].lat), parseFloat(crimes[x].lng) );
marker = new MarkerWithLabel({
title: crimes[x].type,
clickable: true,
position: coordinates,
icon: ' ',
map: map,
labelContent: '<a type="button" class="btn btn-transparent fa fa-ambulance" style="color:rgba(144,15,15,0.8);"></a>',
labelClass: "labels"
});
infobox = new InfoBox({
content: crimes[x].details,
disableAutoPan: true,
isHidden: false,
maxWidth: 150,
pane: "floatPane",
enableEventPropagation: true,
pixelOffset: new google.maps.Size(-140, 0),
zIndex: null,
boxStyle: {
border: "2px solid black",
background: "#333",
color: "#FFF",
opacity: 0.85,
padding: ".5em 1em",
width: "200px",
},
closeBoxMargin: "2px 2px 2px 2px",
closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
infoBoxClearance: new google.maps.Size(1, 1)
});
listener = new google.maps.event.addListener(marker, 'click', function() {
infobox.open(map, this);//I think this is where I'm doing it wrong. How do I tie each specific infobox to its respective click listener?
map.panTo(coordinates);
});
marker.setMap( map );
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
有人知道我哪里错了吗?或者我该怎么做?
我进行了一些重构,它神奇地开始正常工作:
<script>
function initialize()
{
var crimes = <?php echo json_encode($crimes); ?>;
var count = crimes.length;
var uaCampusCoordinates = new google.maps.LatLng( 40.7127837, -74.00594130000002 );
var myOptions = {
zoom: 14,
center: uaCampusCoordinates,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map( document.getElementById( 'map-canvas' ), myOptions );
for(x = 0; x < count; x++)
{
var coordinates = new google.maps.LatLng( parseFloat(crimes[x].lat), parseFloat(crimes[x].lng) );
var markerTitle = crimes[x].type;
var infoBoxContent = crimes[x].details;
marker = createMarker(map, coordinates, markerTitle);
infobox = createInfoBox(infoBoxContent, coordinates);
listener = createListener(map, marker, infobox, coordinates);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
function createMarker(map, coordinates, markerTitle)
{
marker = new MarkerWithLabel({
title: markerTitle,
clickable: true,
position: coordinates,
icon: ' ',
map: map,
labelContent: '<a type="button" class="btn btn-transparent fa fa-ambulance" style="color:rgba(144,15,15,0.8);"></a>',
labelClass: "labels"
});
marker.setMap( map );
return marker;
}
function createInfoBox(infoBoxContent, coordinates)
{
//need to make sure this infobox doesnt get overwritten
infobox = new InfoBox({
content: infoBoxContent,
position: coordinates,
disableAutoPan: true,
isHidden: false,
maxWidth: 150,
pane: "floatPane",
enableEventPropagation: true,
pixelOffset: new google.maps.Size(-140, 0),
//could do a counter for z index to make more relevant markers show up on top, just remember to sort according to relevancy first
zIndex: null,
boxStyle: {
border: "2px solid black",
background: "#333",
color: "#FFF",
opacity: 0.85,
padding: ".5em 1em",
width: "200px",
},
closeBoxMargin: "2px 2px 2px 2px",
closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
infoBoxClearance: new google.maps.Size(1, 1)
});
return infobox;
}
function createListener(map, marker, infobox, coordinates)
{
listener = marker.addListener('click', function() {
infobox.open(map, this);
map.panTo(coordinates);
});
return listener;
}
</script>
相关文章:
- 注册OpenLayers事件时,即使使用匿名函数或绑定,JavaScript关闭也会触发循环内的所有内容
- Jquery将循环变量绑定到getJSON函数
- JsViews复选框从内部循环绑定
- 如何将InfoBoxes与googlemaps实用程序库v3绑定到循环中的点击侦听器
- jQuery使用最后一个参数在Javascript循环中单击绑定函数
- 动态绑定onclick事件为for循环中的所有按钮提供相同的值
- 在绑定到内部$http函数时运行无限摘要循环
- 通过 for 循环绑定会产生错误的结果
- Angularjs 指令,函数双向绑定陷入无限循环
- 使用 jQuery 将事件绑定到具有 for 循环的多个元素
- 余烬每循环加倍.使用内部每个循环变量的值绑定到外部每个循环的变量中命名相同的属性
- 在 Foreach 循环中使用 IF 条件来比较数据绑定值
- Ember.js – 将按钮操作从 #each 循环绑定到其自己的模型
- 绑定一个 onclick 函数,对象作为在循环中创建的锚标记上的参数
- 循环访问 Knockout - 绑定错误中的不同对象的数组
- 循环绑定对象
- 循环绑定以查找循环数
- 通过 for 循环绑定的事件中的数字值错误
- 使用循环绑定事件
- 如何只显示foreach循环绑定中的第一个元素