# How to calculate difference between bounds in Google Maps?

Is it possible, after "bounds_changed" event, to get polygon coordinates covering the difference between new bounds and previous bounds?

Any idea how to do it? Any help would be greatly appreciated.

getBounds() returns a LatLngBounds object that supports equals(), intersects(), contains(), etc.

• intersects() returns boolean value while union() returns LatLngBounds. So intersects() is not so useful. – Xyroid Jul 18 '17 at 9:07

As I am sure you are aware the bounds_changed event occurs after the bounds have changed, therefore the previous bounds are "lost".

I would approach this by having two global variables called prevBounds and newBounds. Then, within my handler for the bounds_changed event I would do the following:

1. Set prevBounds equal to newBounds which will give me my previous bounds
2. Set newBounds equal to the map's current bounds which will give me the new bounds

After the bounds_changed event I would have two variables; one containing my previous bounds and one containing the new bounds of the map. Then you can calculate the difference between them.

• Yes, I know that. I should be more clear in my question. What I wanted help with is this actual calculation between new and previous bounds. I suck at geographic calculations... Anyway, thank you for your answer. – Arek Aug 31 '12 at 11:32
• @Arek I see what you mean now, so what do you actually want to calculate the difference of? Width, Height, Area? Of course if the bounds changed because of a pan then there will be no change in size, just location - what difference would you want to calculate in that case? – CHenderson Sep 1 '12 at 10:48
• Maybe it will be best explained with an image. What I need is the coordinates of the green area. – Arek Sep 3 '12 at 6:28