谷歌地图:只有一个infowindow为多个标记创建

时间:2011-02-12 17:15:56

标签: javascript google-maps

这是我的fiddle链接。

我在地图上添加了标记,也为每个标记添加了信息。 但只有一个信息出现?

2 个答案:

答案 0 :(得分:5)

你走了:

var ships = [['63.44204833', '10.40340333'], ['63.49261667', '9.92661167'], ['63.43243500', '10.37030833'], ['63.43896000', '10.40036167'], ['63.64856000', '10.67950167'], ['63.43330667', '10.36608000'], ['63.43840500', '10.40874000'], ['63.78920833', '11.19232167'], ['63.45155667', '10.20245833'], ['63.43366667', '10.36150000'], ['63.43956667', '10.40019333'], ['63.47066500', '10.33613500'], ['63.43928333', '10.40971667'], ['63.43822000', '10.39873167']];
var map;

var infowindow = new google.maps.InfoWindow({
    content: 'bla'
});

var marker;

function initialize() {
    var myLatlng = new google.maps.LatLng(63.65, 10.65);
    var myOptions = {
        zoom: 9,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.TERRAIN
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

}

function createMarker(lat, lon, html) {
    marker = new google.maps.Marker({
        position: new google.maps.LatLng(lat, lon),
        map: map,
        title: html
    });
    createInfoWindow(marker);
}
function createInfoWindow(m) {
    google.maps.event.addListener(m, 'click', function() {
        infowindow.open(map, m);
    });
}
function processShips(ships) {
    for (var i = 0; i < ships.length; i++) {
        createMarker(ships[i][0], ships[i][1], 'bla');
    }
}

function load(ships) {
    initialize();
    processShips(ships);
}

load(ships);

工作example

答案 1 :(得分:2)

只需在createMarker函数中将标记添加到标记前面

  var marker = new google.maps.Marker({....