javascript - Is there a way to identify which Google Map Marker has been clicked? - Stack Overflow

The follwoing code loops over a json object to place marker on a google map. Which works fine.function

The follwoing code loops over a json object to place marker on a google map. Which works fine.

function displayMarkers(data){
    for(var i = 0; i < data.Lat.length; i++){
        var point = new GLatLng(data.Lat[i],data.Lng[i]);
        marker = new GMarker(point);
        map.addOverlay(marker);

            // here's the problem
        GEvent.addListener(marker, "click", function(){
            alert(data.Name[i]);
        });
    }
}

The problem is, every marker always responds with the last value of "i". I'm stuck trying to figure out a way to identify the marker that has been clicked to somehow get the right information from the data object.

I'm thinking about creating an array of markers at creation to loop through based on the markers location, but that feels really inefficient to me.

Any help, as always, greatly appreciated.

The follwoing code loops over a json object to place marker on a google map. Which works fine.

function displayMarkers(data){
    for(var i = 0; i < data.Lat.length; i++){
        var point = new GLatLng(data.Lat[i],data.Lng[i]);
        marker = new GMarker(point);
        map.addOverlay(marker);

            // here's the problem
        GEvent.addListener(marker, "click", function(){
            alert(data.Name[i]);
        });
    }
}

The problem is, every marker always responds with the last value of "i". I'm stuck trying to figure out a way to identify the marker that has been clicked to somehow get the right information from the data object.

I'm thinking about creating an array of markers at creation to loop through based on the markers location, but that feels really inefficient to me.

Any help, as always, greatly appreciated.

Share Improve this question edited Jan 31, 2014 at 5:34 Kara 6,22616 gold badges53 silver badges58 bronze badges asked May 13, 2009 at 16:52 gargantuangargantuan 8,95416 gold badges70 silver badges110 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

The click event for the map passes three different elements.

GEvent.addListener(map, "click", function(overlay, latlng, overlaylatlng) {
  // overlay = GOverlay or null
  // latlng = GLatLng
  // overlaylatlng = GLatLng or null
});

If the user did not click on an overlay, 'overlay' and 'overlaylatlng' will be null.

Update: You have to add the listener to the map (not the marker) if you want to get a GOverlay out of it. The click event for a GMarker only returns the GLatLng.

The Map supports an onTap event, which passes the index of the marker which was selected, please refer to this example:

It seems that the above solutions only return the coordinates of the marker, which doesn't solve my problem. Perhaps I'm doing something wrong.

However, the way I solved it was to simply add a property to the marker like this

function createMarker(latlng,name) {
      var marker = new GMarker(latlng);

      // HERE WE GO
      marker.value = name;

      GEvent.addListener(marker,"click", function() {
        addToList(this.value);
      });
      return marker;
}

UPDATE: The aboves solves it one way, but the easier way is to attach the event to the map as mentioned in the ments

GEvent.addListener(map, "click", function(marker, point){
    alert(marker);
});

Simple as pie.

    GEvent.addListener(marker, "click", function(o){
            alert(o);
    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745159320a4614315.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信