New GMap2 Object
Version 2 of the Google Maps API is designed to be backwards-compatible with the previous version of the API. What this means in practical terms is that the GMap object (for more information on the GMap object see the Introduction article) can still be used with Version 2. Like the original GMap class, the newly released GMap2 class enables you to create a map. You can create as many map instances as necessary for your application although most commonly this will only be a single instance. Maps are normally embedded within an HTML container called a
GMap2 can also respond to a whole host of events. Events are simply an external stimulus to the map triggered by the user. Typical events might include the user dragging the map or clicking the map, an overlay marker added to the map, or the map type changing from normal to aerial. You can write code that responds to any of these and other events. For instance, you might want to add a marker at the point where the user clicks the map, or perhaps you could display the latitude/longitude coordinates of the center of the map following a pan event.
New Features in Version 2
This latest release of the Google Maps API includes a number of new features. In this section we'll cover these features in more detail.
Overview Map Control (GOverviewMapControl). Let's start with what is probably the most talked about new feature in Version 2: the Overview Map Control. The Overview Map Control adds a collapsible overview map to the bottom, right corner of the screen and displays the geographic extent of the main map with a blue rectangle. The extent of the main map can be controlled by dragging the rectangular extent in the overview map. Doing so will automatically change the current extent in the main map. Take a look at Figure 1 for an example of the Overview Map Control.
Debugging Window. In Version 2, the Google Maps API provides a new GLog namespace that contains some static methods that allow you to write log messages to a log window similar to what you see in Figure 3. This enables basic debugging of your code through the ability to write debug messages. The first call to a GLog method opens a
Two Additional Satellite Zoom Levels. And increased imagery resolution and coverage in the U.S., Canada, U.K., Japan and Europe and almost every major city in the world.
Custom Map Controls. You can define subclasses of GControl to define custom map controls in addition to the built-in controls like GSmallMapControl. Example here.
Custom Map Overlays. Typically you will add user data to your applications through the built-in GMarker and GPolyline classes. However, with the new Version you can define subclasses of GOverlay to create custom map overlays. Example here.
Fewer Memory Leaks. The API now includes a GUnload( ) method that you can call in the unload event of your page to destroy most circular references, including those formed implicitly during event registration. On Google Maps, this has virtually eliminated memory leaks in IE.
Other Significant Changes
GLatLng Replaces GPoint
Prior to Version 2 of the Google Maps API, the GPoint class was used to represent a single, two-dimensional longitude/latitude point of interest somewhere on the face of the earth. However, with Version 2, geographic coordinates are now represented by a new class: GLatLng. GPont now represents a point on a map by its pixel coordinates. This is a significant change that will need to be incorporated by existing Google Maps Version 1 sites and will necessitate changes to those applications.
GLatLng is normally used for two different yet important functions in Google Maps. The first reason for using GLatLng is to set the center point of your map. GLatLng is also used in conjunction with GMarker to create points of interest that are displayed as markers on your map. GMarker must have a coordinate defined by GLatLng to know where to place itself on the map.
Map Type Name Changes
The Map types (Map, Satellite, Hybrid) have new names in Version 2 of the API:
- G_NORMAL_MAP
- G_SATELLITE_MAP
- G_HYBRID_MAP
Zoom level indices in GMap2 start with 0 at the coarsest level, not at the finest level, which was the case in GMap. This allows for maps of arbitrarily high resolution, which allows Google to more easily introduce high resolution imagery. Conversion between old and new zoom level can be accomplished with the following formula:
newZoom = 17 oldZoom
Method Name Changes
On GMap2, new methods have replaced many of the GMap methods. Most of these changes involve the use of the GLatLng class. For more information on the many method changes, refer to the Google upgrade documentation.
Plug: For more comprehensive information on the Google Maps API please see our Google Maps For Your Apps! virtual training course ($75) or our new PDF book, Google Maps API: The New World of Web Mapping ($12.99 on its own, or free with the course).