theMechanism | Google Maps API Demo 3

Add controls and render the map.

Adding controls is simple: list of built-in controls: Google Maps API Reference . custom controls

The setCenter method takes three parameters: a point object [required], a zoom level [optional], and a map type constant [optional].

To center, we use the GLatLng class to create a point object. GLatLng takes two parameters: latitude and longitude. The zoom level is an integer [0 is entire planet, 17 is as close as you can get], and the three map type constants are: G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP. These map types correspond to Google's base maps: a street map, a satellite map, and a hybrid of the two.

Code

JavaScript

		var objMap = '';
		if (GBrowserIsCompatible()) {
			objMap = new GMap2(document.getElementById('map'));
			objMap.addControl(new GLargeMapControl());
			objMap.addControl(new GMapTypeControl());
			objMap.addControl(new GMapScaleControl());
			objMap.addControl(new GOverviewMapControl());
			objMap.setCenter(new GLatLng(40.753808, -73.995924), 13, G_NORMAL_MAP);
		} else {
			alert('Please either upgrade to a browser that supports JavaScript or enable JavaScript now. Thanks!');
		}
	

previous . next