简体   繁体   中英

Google maps API V3: how to set same boundaries for panning, whatever the zoom level

I have created a custom map (fantasy map) with google map api V3 and, by default, the panning is unlimited. My map beeing very limited, I don't want the user to be able to pan outside it, whatever the zoom.

I read a thread about it here: How do I limit panning in Google maps API V3?

It's good but, somehow, it doesn't work well for every zoom level.

You can see the problem very clearly here with the same code: http://econym.org.uk/gmap/example_range.htm

On this example, at the default zoom level, you can see some lands on the extreme top right of the map ("Bergen"). But the more you zoom, the more these lands get out of the map and become unreachable. That's exactly what I get with my own map with the same script... If I set boudaries at my default zoom level, the more I zoom, the more the edges of the map get out of reach.

Is there any workaround in order to keep the exact same boudaries, whatever the zoom level?

Thank you.

I'm not 100% sure that i'm following your questions.. but I'll try. The way this example works is to test that the centre of the view is in the bounds... so the bounds is actually smaller then Bergen... The upper right corner of the limit is actually in the ocean in the centre of the view at the at zoom.. so when you zoom in, the centre is in the same spot and the upper right becomes closer.

The solution would be to replace the centre check with a bounds check..

if((allowedBounds.contains(map.getBounds()))...

The you can then just map.panToBounds(allowedBounds) if you're outside.

Should work.. give it a try and let us know if you still have problems and I'll take a closer look.

EDIT: wow.. old question... oh well :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM