Zooming in to a group of markers

38 views
Skip to first unread message

Gerry Orkin

unread,
Jan 25, 2012, 8:43:07 AM1/25/12
to google-map...@googlegroups.com
I'm showing multiple markers on a map (see example http://whothru.com/aroundme.html), but I have no idea how to have the map zoom in to show just the area bounding the markers. I've seen a few examples, but I have no idea how to integrate them into my code...because I barely know what I'm doing. 

Can anyone help?

Cheers

Gerry

sb

unread,
Jan 25, 2012, 3:06:26 PM1/25/12
to Google Maps JavaScript API v3
Try fitbounds.

You will need to put this at the end of your code;
var myBounds = new google.maps.LatLngBounds();
var p1 = new google.maps.LatLng(25.774252, -80.190262);
var p2 = new google.maps.LatLng(18.466465, -66.118292);
var p3 = new google.maps.LatLng(32.321384, -64.75737);
myBounds.extend(p1);
myBounds.extend(p2);
myBounds.extend(p3);
map.fitBounds(myBounds);

Just be sure to change the lat/lng numbers to match your own markers.

http://www.shanebest.com/api3/fitbounds.html

On Jan 25, 8:43 am, Gerry Orkin <gerry.or...@gmail.com> wrote:
> I'm showing multiple markers on a map (see examplehttp://whothru.com/aroundme.html), but I have no idea how to have the map

Gerry Orkin

unread,
Jan 25, 2012, 7:20:40 PM1/25/12
to google-map...@googlegroups.com
Shane, that's 99.8% perfect :)  I just realised, however, that in rare cases there will be only one marker on the map. Your code zooms into a boundary around that marker, but it is zoomed in too much - you can't see the context. Is there a way to trap for a single marker and do something else?

Thanks again!

Gerry

geoco...@gmail.com

unread,
Jan 25, 2012, 9:19:36 PM1/25/12
to Google Maps JavaScript API v3
Sure. Count the number of markers you add to the bounds. If it is 1,
do the special case...

-- Larry


>
> Thanks again!
>
> Gerry

Gerry Orkin

unread,
Jan 26, 2012, 1:50:38 AM1/26/12
to google-map...@googlegroups.com
Larry, I understand the logic, but I'm clueless as to how to write that :)

g

geoco...@gmail.com

unread,
Jan 26, 2012, 2:19:44 AM1/26/12
to Google Maps JavaScript API v3
On Jan 25, 10:50 pm, Gerry Orkin <gerry.or...@gmail.com> wrote:
> Larry, I understand the logic, but I'm clueless as to how to write that :)
>
> g

looking at your example, how would you tell when there is only one
marker?

when that case occurs, center the map on that marker, and set the zoom
to a number you like.

-- Larry

Gerry Orkin

unread,
Jan 26, 2012, 3:12:51 AM1/26/12
to google-map...@googlegroups.com
Oh right..silly me. I'm writing the html file from my iOS app, so I just write it without the bounds stuff and with zoom and centre stuff. D'oh. Honestly, I know enough to mess things up, but not enough to create or fix 'em :)

Thanks for your advice!

Gerry

> --
> You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
> 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