tags:

views:

650

answers:

3

Hi..is that possible, suppose if I search any random city in google map and google map automatically set that zoomlevel ?I mean How can I set zoom level of google map automatically ?

I am using google map API.

please help me.

A: 

When you instantiate a google maps object using: new google.maps.Map(domElement, options)

You can can use options to set (among other things) the zoom level.

The default zoom level can be set using { zoom: 14 } for example.

After you create you google maps object, you can change the zoom level using setZoom()

var myMap = new google.maps.Map(
    document.getElementById('map-canvas'), 
    { zoom : 14 } 
);

myMap.setZoom(12);
Koobz
This is not what the user asked. He was asking for an *automatic* way to do this.
Vlad Zloteanu
A: 
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key=abcdefg&amp;sensor=true_or_false"
            type="text/javascript"></script>
    <script type="text/javascript">

    function initialize() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        map.setUIToDefault();
      }
    }

    </script>
  </head>
  <body onload="initialize()" onunload="GUnload()">
    <div id="map_canvas" style="width: 500px; height: 300px"></div>
  </body>
</html>

The map.setCenter() function can be used to set the zoom level i.e map.setCenter(GLatLng coordinate, zoom level)

zapping
This is not what the user asked. He was asking for an *automatic* way to do this.
Vlad Zloteanu
+2  A: 

See: http://stackoverflow.com/questions/2265055/how-to-get-google-maps-api-to-set-the-correct-zoom-level-for-a-country

In summary, you use a Geocoder object to getLocations by a query string (", "). Then use a returned PlaceMarker object to define a LatLngBounds object. Use the LatLngBounds object as a paramter for getBoundsZoomLevel to set the zoom level.

krishnaz