从外面打开标记/ infowindow?

时间:2011-11-21 08:28:56

标签: google-maps google-maps-markers infowindow

我正在为我的Google地图添加标记,如下所示:

    function createMarker(location, info) {
        var marker = new google.maps.Marker({
            map: map,
            position: location
        });

        google.maps.event.addListener(marker, "click", function () {
            if (infowindow) infowindow.close();
            infowindow = new google.maps.InfoWindow({ content: info, title: 'test' });
            infowindow.open(map, marker);
        });
    }

如何在不手动点击地图的情况下访问和打开标记?

我尝试保存marker对象并执行marker.click(),但似乎无效。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

添加了:

var markers = new Array();

function getMarkerbyLocation(location) {
    for (var i = 0; i < markers.length; i++) {
        if (markers[i].getPosition().lat() == location.lat() && markers[i].getPosition().lng() == location.lng())
            return markers[i];
    }
}

修改:

function createMarker(location, info) {
    var marker = new google.maps.Marker({
        map: map,
        position: location
    });

    google.maps.event.addListener(marker, "click", function () {
        if (infowindow) infowindow.close();
        infowindow = new google.maps.InfoWindow({ content: info });
        infowindow.open(map, marker);
    });

    // Modification here!
    markers.push(marker);
}

页面上的某些事件执行以下操作:

google.maps.event.trigger(getMarkerbyLocation(new google.maps.LatLng(53.43306, 9.98556)), 'click');