Openinfowindowhtml on google map after getting information from server

I have a google map on my site and attach a moveend event handler to it

   GEvent.addListener(map, "moveend", function() 
    {
            map.clearovrelays();
            GetLayerDataFromServer(); //it set the markers again on the map  according the map position
    });

      

and also i have an event handler for clicking on a marker

GEvent.addListener(marker, 'click', function()
    { 
        marker.openInfoWindowHtml('this is the data');
    });

      

My problem is this

When the user clicks on one of the markers on the map he opens the openInfoWindowHtml of the corresponding marker.

And it also moves the map to the marker position. And then it raises the event

map.moveend

      

And in the case of map.moveend, I clear all the markers on the map and reload them according to the new map.

As a result, when the user clicks on the marker, it opens up to its second indoWindowHtml and it clears the map and loads the markers again without showing the indoWindowHtml of the clicked marker.

My question is, what should I do to show infoWindowHtml?

+2


source to share


3 answers


You can set a flag that indicates if the user clicked the marker and did not clear the map if it did.



var marker_clicked = false;

GEvent.addListener(map, "moveend", function() 
{
    if(!marker_clicked)
    {
        map.clearovrelays();
        GetLayerDataFromServer(); //it set the markers again on the map  acording the map position
    }
    marker_clicked = false;
});

GEvent.addListener(marker, 'click', function()
{
    marker_clicked = true;
    marker.openInfoWindowHtml('this is the data');
});

      

+1


source


One possible alternative strategy is to open your info window with {suppressMapPan: true}, which tells the map not to pan when the infowindow opens. This way, you know that any card movements are valid card movements by the user.



Note: {suppressMapPan: true} is undocumented, so it may disappear in some future releases.

0


source


Another strategy would be to write

GEvent.addListener(marker, 'click', function()
    { 
        var iwAnchor = marker.getIcon().infoWindowAnchor;
        var iconAnchor = marker.getIcon().iconAnchor;
        var offset = new GSize(iwAnchor.x-iconAnchor.x,iwAnchor.y-iconAnchor.y);
        map.openInfoWindowHtml(marker.getLatLng(),'this is the data',{pixelOffset:offset});
    });

      

and then instead of calling clearOverlays (), loop through the markers one by one, removing them.

By opening a window on the map instead of a marker, it does not close automatically when the marker is deleted.

The infoset is now an overlay, so clearOverlays removes it, so you can't use clearOverlays. It may seem inefficient to loop through the markers by removing them one by one, but clearOverlays does a very similar loop inside.

The above iconAnchor layouts just set the infowindow value to the same location it would be if you used marker.openInfowindowHtml, not at the bottom of the marker.

0


source







All Articles