Sunday, July 22, 2007

Setting the default map type in Google Maps

So I ran into a silly bug trying to set the default map type (default, satellite, or hybrid) on a google map. Basically I kept getting an error and the map wouldn't load even though I triple checked the constant's name, etc. Turns out that the order that you call setMap commands matters. Turns out that the setCenter() command must be given first before any other alterations to the map can be done. Here's the code for setting the map to satellite view by default:

map.setCenter(new GLatLng(36.739067, -77.905517), 5);
var givenmaptypes = map.getMapTypes();
map.setMapType(givenmaptypes[1]);


I think you can set the maptype within the setCenter function call as well. This Google Maps API thread was useful as well.

2 comments:

Anonymous said...

Fantastic - just what I needed. Thank you and thank Google!

NOTE: to set map type, you don't have to call getMapTypes() first. You can just add this line after calling set center:

map.setMapType(G_SATELLITE_MAP);

Unknown said...

You can set the map type in the map.setCenter command also:

"
The parameters of map.setCenter() are

1. A GLatLng() object specifying the geographical location of the center of the map.
2. The zoom level.
3. The map type.

All three parameters are optional. To skip a parameter, use "null",

E.G.

map.setCenter(null,12,G_SATELLITE_MAP)"

taken from: http://econym.googlepages.com/index.htm