Coded – Web Development and Programming Blog

C#, ASP.NET, Google, Remoting, AJAX, Silverlight, Web Development

How to auto zoom and auto center Google Maps

Posted in User Interface, Web Programming by Andrei Alecu on the September 28th, 2007

This post discusses how to auto center and auto zoom a Google Map to display all of the markers optimally.

In order to get accomplish this, you have to calculate the center point and zoom level manually via code. Fortunately this is pretty easy to do, you just need to calculate the minimum and maximum latitude/longitude of all of your markers and then get the center point of the resulting area (which will be a rectangle).

I’m using the Google Maps for ASP.NET control by Subgurim, but this method should apply to any other control, and should even work with plain JavaScript.

Auto center

First, you will need to find out the min/max latitude and longitude for your markers, this can usually be done in one pass at the same time as you add them. Here’s some Visual Basic.NET code to help you out (I’m using VB.NET for this project at the request of my collaborator, but it should be easy to convert to C# – think of it as pseudo code):

You then just need to calculate the center point of the resulting area. This is as easy as:

You can now center your map widget to these coordinates.

Auto zoom

The auto zoom part is where it gets a little tricky. In my application I’m calculating the distance in miles from the minLatitude, minLongitude coordinate to the maxLatitude, maxLongitude coordinate.

The formula for calculating the distance in miles between two coordinates is:


        miles = (3958.75 * Math.Acos(Math.Sin(latitude1 / 57.2958) * Math.Sin(latitude2 / 57.2958) + Math.Cos(latitude1 / 57.2958) * Math.Cos(latitude2 / 57.2958) * Math.Cos(longitude2 / 57.2958 - longitude1 / 57.2958)))

By calculating how many miles your markers span, you can make an informed guess on which zoom level to use. For instance, if the distance between the furthest two markers is less than 0.2 miles, then you could use the maximum zoom level available—which is 16 by the way—; if the distance is more than 0.2 miles and less than 0.5 miles—use zoom level 15, and so on.

Here’s a list of zoom levels for distances up to 15 miles:

That’s it, your map should now be able to auto zoom and auto center itself on your markers!

[tags]Google Maps API, Visual Basic.NET[/tags]

Bookmark on del.icio.us

8 Responses to 'How to auto zoom and auto center Google Maps'

Subscribe to comments with RSS


  1. on July 15th, 2008 at 6:57 am

    Hi.

    That is really a very nice and helpful post.


  2. on August 12th, 2008 at 3:38 am

    great post…i love it

  3. Supriya said,

    on December 20th, 2008 at 8:20 am

    Hello!

    i have used ur code but implemented in javascript,but it give error
    c.getMinimumResolution(); is not a function.
    One more thng i found ;this code only work for 2 markers but i want to
    do same auot-center & auto-zoom for many markers.
    Plz give me suggestion for error as well as my other problem.
    Thanks!

  4. Nick Warzin said,

    on January 20th, 2009 at 1:54 pm

    For anyone still catching this article on Google searches, GLatLngBounds takes care of all this math if you don’t mind doing some of the work in JavaScript.

    http://code.google.com/apis/maps/documentation/reference.html#GLatLngBounds

  5. Salman said,

    on March 17th, 2009 at 3:28 am

    Since Google Maps API v2, you can use the GLatLngBounds class for this purpose. I’ve put together a demonstration at this URL:

    http://911-need-code-help.blogspot.com/2009/03/zoom-to-fit-all-markers-polylines-or.html

  6. Ben said,

    on September 30th, 2009 at 5:17 pm

    Just to say thanks for the maths for Auto Zoom :)

    I’m learning Ruby On Rails at the moment and I popped in to a little app to save me countless hours! As I’m still a beginner I couldn’t quite figure out how to use GLatLngBounds but, until I do, your method will suit me just fine :)


  7. on January 19th, 2010 at 7:44 am

    I’ve recently done this in Javascript, but it’s great to see it done in C#. Great post!

  8. coder said,

    on August 13th, 2010 at 7:11 pm

    Thanks for posting this and thanks for the source code! :-)

Leave a Reply