How to zoom to a specific LatLng, without re-centering

34 views
Skip to first unread message

pinksy

unread,
Oct 15, 2011, 3:27:17 PM10/15/11
to google-map...@googlegroups.com
Hi. Apologies if this has been asked before, but I can't find it. 

I want to find a way to zoom in to a location that isn't the center of the map, without having to re-center the map. For example, the maps at http://maps.google.com/ have a context menu, where you can select "Zoom in" on any point on the map, and it will zoom to that point. I can't see a way to achieve a similar effect in V3 of the JS API. The zoom functions all seem to zoom to the center of the map. Does anyone know?

Thanks

Joseph Elfelt

unread,
Oct 15, 2011, 11:34:30 PM10/15/11
to Google Maps JavaScript API v3
In your map options, include:
scrollwheel: true,

Then you can point to any spot and zoom in/out on that spot with the
mousewheel. The spot under your cursor stays at the same location on
your screen.

pinksy

unread,
Oct 16, 2011, 6:30:56 AM10/16/11
to google-map...@googlegroups.com
Thanks Joseph.

Is there any way of doing it without the scrollwheel? The zoom option from the context menu in http://maps.google.com/ does it without a scrollwheel.

Ideally I want to be able to specify a LatLng point programmatically, and call a zoom function to zoom about that point. I noticed that V2 of the API has something similar: GMap2.zoomIn(latlng?:GLatLng, doCenter?:Boolean, doContinuousZoom?:Boolean)

Cheers

Jeremy Geerdes

unread,
Oct 16, 2011, 7:10:05 AM10/16/11
to google-map...@googlegroups.com
You could certainly do it programmatically. That's how it's done with the scrollwheel, and though I never used the v2 zoomIn method as described, it would be how it was done there, too. Basically, you would need to take the LatLng you're wanting to zoom around and calculate a new center point based on the following formulae:

newCenterLat = givenLat + ((currentCenterLat - givenLat) * Math.pow(.5, currentZoom - newZoom));
newCenterLng = givenLng + ((currentCenterLng - givenLng) * Math.pow(.5, currentZoom - newZoom));

You would then call something like this:

map.setOptions({
 center : new google.maps.LatLng(newCenterLat, newCenterLng),
 zoom : newZoom
});

And you should zoom in or out around the given point. I.e., the given point should remain fixed within your display, regardless of where it is on the map.

Jeremy R. Geerdes
Generally Cool Guy
Des Moines, IA

For more information or a project quote:

If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church!

--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-maps-js-api-v3/-/7hfiYFWhVwgJ.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.

Reply all
Reply to author
Forward
0 new messages