How to clear fitbounds in google maps javascript

I am using the watchposition method for google maps. Each time it calls map.fitbounds(bounds), where bounds is an array of lat lng objects I have built. However, if the user drags on the map or zooms, watchposition causes it to snap back to it's bounds. I want to test if it has been dragged or zoomed, and if it has, I do not call fitbounds. So I need to clear the bounds of the map and allow it to remain as it is, if someone has dragged or zoomed. I tried this:

if (dragged == false && zoomChanged == false) {
else { map.fitBounds(); }

Open in new window

And it doesn't like map.fitbounds(). So what should I do to tell the map to remain at the dragged to or zoomed to bounds that the map has after the event instead of snapping back to the bounds that were built?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
Doesn't make sense. You initialize a map and set the bounds to include all LatLng objects in an array by calling map.fitBounds(). DONE! No reason to call map.fitBounds() again. Are you re-initializing the map every time watch position changes? Why?
BobHavertyComhAuthor Commented:
Hi Tom, nice to hear you. Thanks for your response. The reason why I am clearing them and rebuilding them is because there is one marker moving all of the time based on watchposition, and there are other markers on the map that can be added or deleted, or their lat lng could change based on an edit in the app ui. So the map not only needs to expand when needed, based on x amount of semi stationary markers and a constantly moving marker, it also needs to retract as each marker is removed. Or if the moving marker is an outermost marker, the map bounds retract if the marker moves back within the current bounds.

Then there is the added complication that if a user drags or zooms on the map, we have to tell it to stop snapping back to it's bounds with every watchposition call. And if the user hits a "fit" button, the map snaps back to it's bounds and continues snapping to them until there is another drag or zoom.

Not a trivial problem for me.
Tom BeckCommented:
Seems like you could add an event listener for dragend and zoom_changed, set a variable and test for it before calling map.fitBounds(bounds);
google.maps.event.addListener(marker, 'dragend zoom_changed', function() {
    stopFit = true; //example of an in-scope variable to test for before calling fitBounds()

Open in new window

Before calling fitBounds(), test:
if (! stopFit) {

Open in new window

Then reset stopFit to false when the "Fit" button is clicked and rebuild the map.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.