在Google地图中添加标记阵列 [英] Adding Array of Markers in Google Map
本文介绍了在Google地图中添加标记阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一些帮助才能在Google地图上创建一系列标记,以便它可以更高效,而不是逐个创建标记。我尝试了以下,但它不起作用。任何人都有建议?
I need some help in creating an array of markers in Google Map so that it can be more efficient, instead of create a marker one by one. I tried the following but it doesn't work. Anyone have advice?
//create array to store a set of location
var collection = new Array();
//a set of locations stored in array
collection[0] = new google.maps.LatLng(13.742167701649997, 100.50721049308777);
collection[1] = new google.maps.LatLng(13.74428, 100.5404525);
collection[2] = new google.maps.LatLng(13.744108, 100.543098);
var pointMarkerImage = new Array();//store image of marker in array
var pointMarker = new Array();//store marker in array
//create number of markers based on collection.length
function setPoint(){
for(var i=0; i<collection.length; i++){
var pointMarkerImage[i] = new google.maps.MarkerImage('marker.png');
var pointMarker[i] = new google.maps.Marker({
position: collection[i],
map: map,
icon: pointMarkerImage[i],
animation: google.maps.Animation.BOUNCE,
title: "collection"+ i
});
google.maps.event.addListener(pointMarker[i], 'click', function() {
window.open("blog/page01.html","_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=yes")
};
);
}
}
推荐答案
是我的简单代码,它工作正常。当您点击标记时,它会根据标记的位置分别打开信息窗口。
This is my simple code and it works fine. When you click on marker, it will open the Info Window respectively based on that marker's location.
<script>
var locations = [
['Title A', 3.180967,101.715546, 1],
['Title B', 3.200848,101.616669, 2],
['Title C', 3.147372,101.597443, 3],
['Title D', 3.19125,101.710052, 4]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: new google.maps.LatLng(3.171368,101.653404),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow;
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
<div data-role="page" id="map_result">
<div data-role="header" data-position="fixed"><h1>Multiple Marker</h1></div>
<div data-role="content" style="padding:0;">
<div id="map"></div>
</div>
</div>
这篇关于在Google地图中添加标记阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文