在Google Map中添加标记数组

问题描述 投票:11回答:3

我需要一些帮助来在Google地图中创建一系列标记,以便它可以更高效,而不是逐个创建标记。我尝试了以下但它不起作用。有人有建议吗?

//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")
             };
    );
    }
}
javascript google-maps-api-3
3个回答
30
投票

这是我的简单代码,它工作正常。当您单击标记时,它将根据该标记的位置分别打开信息窗口。

<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>

3
投票

我看到一些事情:

1)在for循环中,var pointM ...应该只是pointM ...添加var使它忽略你在for循环之外的集合。

2)你有一个;在一个破坏事物的功能之后。

试试这个:

//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++){

    pointMarkerImage[i] = new google.maps.MarkerImage('marker.png');
    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");
    }
    );
  }
}

1
投票

我复制了齐齐阿巴齐兹先生,并改善了一点。这是完整版

<script type="text/javascript">

function initMap() {

    var locations = [
         ['Title A', 3.180967,101.715546],
         ['Title B', 3.200848,101.616669],
         ['Title C', 3.147372,101.597443],
         ['Title D', 3.19125,101.710052]
    ];
    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>

<script async defer
        src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=YOURAPIKEY=initMap">
</script>


<style type="text/css">
  #map { height: 400px;
    margin-left:auto;
    margin-right:auto;
    text-align:left;
    width: 80%;}
</style>

<div id="map"></div>
© www.soinside.com 2019 - 2024. All rights reserved.