1

I have a click event that grabs coordinates remotely, lays them on a map, and connects them with a polyline. I have other click events that do the same, but want to keep them as a separate layer so that the user can toggle to view each of the clicks they have done. Does anyone know of a good way to group them separately to do this or another approach?

Here is my code so far:

drawPoints: function (points, color) {
    $.each(points, function (key, val) {
        var location = new google.maps.LatLng(val.Latitude, val.Longitude);
        MyTest.addMarker(location, val.Name, val.Description);
        MyTest.coordinates.push(location);
    });

    this.path = new google.maps.Polyline({
        path: MyATest.coordinates,
        strokeColor: color,
        strokeOpacity: 1.0,
        strokeWeight: 2
    });

    this.path.setMap(this.googlemap);
},

addMarker: function (location, title, html) {
    var marker = new google.maps.Marker({
        position: location,
        map: this.googlemap,
        title: title,
        html: html
    });

    this.markers.push(marker);
},

// Removes the overlays from the map, but keeps them in the array
clearOverlays: function () {
    if (this.markers) {
        for (i in this.markers) {
            this.markers[i].setMap(null);
        }
    }
},

// Shows any overlays currently in the array
showOverlays: function () {
    if (this.markers) {
        for (i in this.markers) {
            this.markers[i].setMap(map);
        }
    }
},
4

1 回答 1

1

您已将它们保存在 this.markers 中 - 这是一个很好的开始。现在您需要做的就是click为每个标记定义一个侦听器函数:

for(i in this.markers) {
    google.maps.addListener(this.markers[i], function() {
        this.markers[i].clicked = true; });
}

然后,每当用户只想切换他们单击的标记时,循环遍历您的标记(就像您正在做的那样)并将所有 this.markers[i].clicked === 未定义的地方设置为 null。

于 2012-05-04T11:31:21.953 回答