Do not fire 'zoom_changed' event when fitbounds() is in used - google-maps

In my init i have some thing like this
google.maps.event.addListener(gmap, 'zoom_changed', function(){
GMapPlot.getSwNe(window.gmap.getBounds());
//GMapPlot.rePlotMap(presentPlaceType);
});
and at some point i have gmap.fitBounds(GMapPlot.markerBounds);, my question is fitBounds fires a zoom_changed event that intern invokes the bound method, i don't want this to happen, is there a way to do that. If i try to clearListener for zoom_changed event before that fitBounds is not working.

Related

How to remove and then activate an addListener?

I need to remove a certain event and then activate it.
What happens is as follows:
I have a polygon and with the "mouseover" event opens the InfoWindow, and with the "mouseout" event the InfoWindow closes.
So far so good, the problem is when I'm doing the "drag" event in polygon, "InfoWindow stays in the same place and does not follow the polygon.
So I need to close InfoWindow so the polygon is in the "drag" event, and activate only another time.
How do I do this?
The event namespace has everything you need, such as clearListeners and removeListener methods.
The Polygon class has a dragStart, a drag and a dragEnd event.
Therefore you should be able to:
Add your listeners
Listen for a drag or the start of a drag
Close the infowindow / remove your listeners
Listen for the end of a drag
Add your listeners back
I hope this makes sense.
As a side/personal note and speaking in terms of user experience, I am quite convinced that opening/closing stuff based on mouse movements is not a great idea... but that's just me maybe.

Google Map API V3 Polygon DragEnd Event

We are trying to add a dragend event to a polygon so that when the user release the mouse, something happens. However, the dragend on the polygon seems to fire like drag. i.e. fires continously and not just once when the dragging has finished.
google.maps.event.addListener(overlay, 'dragend', function () {
// Do something just once when dragging has ended.
});
Anyone come across this issue. Is this a bug?
Many thanks.
You can try to add the event listener like this:
overlay.addListener('dragend', function () {
console.log('Drag end!');
});
Take a look at this example. It might work this way.

When programatically adding google map shapes need way to add rightclick listener event

From the examples I don't see a way to add a rightclick event on a shape I created on the map programatically. I'm creating maps that are saved to a database, so I'm restoring the shapes. I see an example where they added a click event within the 'overlaycomplete' listener event in examples but how do you do this if you are creating the shape?
You can also do it now in a slightly more succinct way:
newShape.addListener('rightclick', function () {
alert('Right clicked!');
});
So the answer is easy. I just added this code to add the listener:
google.maps.event.addListener(newShape, 'rightclick', function (e) {
alert('Right clicked!');
});
Where newShape is the new created polygon.

Google Maps API rectangle bounds_changed event firing on resize and move

I am working with the GoogleMaps API and I'm trying to distinguish between a rectangle object being resized or moved.
I am using the listener like so:
google.maps.event.addListener(newShape, 'bounds_changed', function() {
// do stuff
});
However, this will fire when the rectangle is resized AND when it is moved completely. Is there some way to distinguish between these two distinct events?
I tried the way #Darwin has suggested. When we just begin to drag the coordinates still remain the same and so the event is fired for drag as well as bounds_changed
An easier and more reliable way, I did is to have a global variable called isBeingDragged and set it to true in dragstart event handler. In the bounds_changed event handler, I checked if this variable is true. If it is not, its a resize event, else it is a drag event.
I again set the isBeingDragged variable to false in drag_end event handler.
Store the bounds of the rectangle initially, when the event fires check if both(soutWest and NorthEast) have been changed. When It does , the rectangle has been moved, otherwise it has been resized.
After the check update the stored bounds.

Google maps v3 zoom end

I have a problem with google maps v3 zoom_change, as it doesn't solve my need perfectly.
I have 2 unmet requirements:
I need to get the bounds and show points that are inside the bounds after the user clicked in zoom control and zoom has been reajusted in the map. The method zoom_change do it before reajusting zoom, and the bounds aren't the ones I need.
The method zoom_change is called every time, for example, whenever I execute fitBounds or setZoom. I only need it when when zoomcontrol is clicked or when mousewheel is moved.
Is there a solution available in the v3 API for those issues?
Listen for the idle event.
From Google Maps Javascript API V3 Reference:
idle: This event is fired when the map becomes idle after panning or
zooming.
The event is called zoom_changed (NOTzoom_change). The event handler of this event is called AFTER the zoom has changed. It is indeed not straightforward to distnguish the zoom change caused by the user from that one caused by the program. A possible solution is to maintain a "global" variable say userZoom which denotes whether the user triggered the zoom.
var userZoom = true; // initial value: be prepared for user action
// Install listener
google.maps.event.addListener(Map.self, 'zoom_changed', function() {
if (userZoom) {
// the user changed zoom: do what should be done
}
else {
// zoom change caused by a program action: ignore
}
userZoom = true; // be prepared for the user zoom action
});
Before you call any of the program actions that change the zoom, set userZoom = false, e.g.
userZoom = false;
map.setZoom(9);
The only real way I have found is to create a custom zoom control on the map,
https://developers.google.com/maps/documentation/javascript/controls?utm_source=welovemaspdevelopers&utm_campaign=stackoverflow#CustomEvents
and then set an event listener on the control,
https://developers.google.com/maps/documentation/javascript/controls?utm_source=welovemaspdevelopers&utm_campaign=stackoverflow#ControlModification.
Clearly not elegant, but an option.