APIv2
From Google Mapki
API v2 (beta) Version 2.31 of the API has removed (renamed) many of the known properties and methods to shorten the names.
"We want to emphasize that the motivation for the renaming is to decrease the JavaScript code size.", The Google Maps Team
This page is meant to help developers get an overview of the new version of the API in order to best prepare for the transition and hopefully give some feedback to the Google Maps Team about features developers would like to see available.
Contents |
Using the API v2
The API versioning system now supports v2.
If you change the line that loads the API code to specify &v=2, you'll get version 2 code.
There are some examples of maps using the version 2 interface at econym.googlepages.com/index.htm, and a complete Unofficial Reference to everything that's accessible under the version 2 interface.
The VML style is no longer required. You may remove the
<style type="text/css">
v\:* {behavior:url(#default#VML);}
</style>
from the head for version 2 pages. The API code inserts the equivalent style as necessary. (You still require the xmlns:v="urn:schemas-microsoft-com:vml" attribute in the html element.)
Documented or frequently used undocumented V1 Methods and Properties which are no longer available:
GMap
.infoWindowEnabled
.enableInfoWindow
.disableInfoWindow
.openInfoWindowXslt
.closeInfoWindow - map.getInfoWindow().hide() seems to work as a replacement
.getSpanLatLng - replaced by getBounds().toSpan()
.centerAndZoom - replaced by setCenter
.recenterOrPanToLatLng - replaced by panTo
.hideOverlays
.showOverlays
.spec.getLowestZoomLevel - replaced by getBoundsZoomLevel
GMarker
.openInfoWindowXslt
.display - use function here to acheive the same effect
.setZIndex
GPolyline
.display
GMapType (available as map.getCurrentMapType())
.getShortLinkText
.numZoomLevels
.getAlternativeMapType
.baseUrls
.lowResBaseUrls
.overlayBaseUrls
.lowResOverlayBaseUrls
GXml (completely removed)
Verified new Methods and Properties in V2:
GMap
.openInfoWindowTabsHtml
.openInfoWindowTabs
.panTo(latlng) renamed from map.recenterOrPanToLatLng()
Pans or jumps to the target location
.savePosition() renamed from map.saveMapState
Saves the map centre and zoom level information which
gets used when the user clicks on the
"Return to last result" icon
.returnToSavedPosition() Resets the map to the values saved by map.savePosition()
.zoomOut() Zooms out by one zoom level
.zoomIn() Zooms in by one zoom level
.getCenter() replaces getCenterLatLng()
Returns a GLatLng() of the centre of the map
.setCenter(latlng?, zoom?, maptype?)
If the first parameter is not null, set the map centre to the location
If the second parameter is not null, set the zoom level
If the third parameter is not null, set the map type
E.g. map.setCenter(new GLatLng(51,-2), 8, G_HYBRID_MAP)
.setZoom(zoom) Sets the zoom level.
Identical to calling map.setCenter(null,zoom,null)
(NB. zoom levels now run in the opposite direction)
.getContainer() replaces direct access to the map.container property
Returns a reference to the div that contains the map
.getBounds() replaces map.getBoundsLatLng()
returns a GBoundsLatLng of the visible region
.getBoundsZoomLevel(latlngbounds) replaces map.spec.getLowestZoomLevel()
See below for how to use it
.checkResize() renamed from map.onResize()
.getZoom() renamed from map.getZoomLevel()
(NB. zoom levels now run in the opposite direction)
.getSize() returns a GSize() of the size of the map container
GMarker
.openInfoWindowTabsHtml
.openInfoWindowTabs
GMapType
MapType names have changes in version 2. The names are now:
- G_NORMAL_MAP
- G_SATELLITE_MAP
- G_HYBRID_MAP
The map types are loaded in this order by default.
GMapType methods and properties:
.getCopyright()
.getErrorMessage()
.getLinkColor()
.getMaximumResolution()
.getMinimumResolution()
.getName()
.getProjection()
.getTextColor()
.getTileLayers()
.getTileSize()
.getUrlArg()
.getBoundsZoomLevel()
.getSpanZoomLevel()
.projection
Changed Parameters
GMap
.addControl(control,position) The third parameter is removed
The printing of controls is now managed by the
control.printable() method
.pan(x,y) Pans the map the set amount of pixels. This can be seen when
calling map.pan(x,0) where x is 1/2 of the map's div tag's
width
Verified New Classes in V2
GLatLng
One major design shift is that GPoint() is no longer used to identify a geographical location. GPoint() still exists, but it is only used for specifying positions of pixels on the screen. Geographical locations are specified with the new GLatLng() object.
Constructor
new GLatLng(latDegrees,lngDegrees)
- Identifies a geographical location
- (Note that the latitude and longitude are in the opposite order from GPoint)
Methods
- lat()
- Returns the .latDegrees property
- lng()
- Returns the .lngDegrees property
- latRadians()
- Returns the latitude expressed in radians
- lngRadians()
- Returns the longitude expressed in radians
- distanceFrom(latlng)
- Returns the distance expressed in metres from the specified location.
- (Note that GPoint.distanceFrom(point) in version 1 returned the distance expressed in "degrees")
- equals(latlng)
- Returns true if the two latlngs identify the same location
- toString()
- Returns a string in the format "(1.123456789,-1.123456789)"
- toUrlValue()
- Returns a string the format "1.123457,-1.123457". The values are rounded to 6 decimal places.
GLatLngBounds
The GBounds() class no longer exists. It is replaced by GLatLngBounds().
Constructor
new GLatLngBounds(latlng?,latlng?)
- Identifies a rectangular geographical region
- If two parameters are passed, they are taken to represent the SouthWest and NorthEast corners of the rectangle. The corners must be in the right order for it to work correctly.
- If one parameter is passed, the region contains just that point.
- If no parameters are passed, the regions is set to the empty region GLatLngBounds(new GLatLng(57.29577951308232, 180), GLatLng(-57.29577951308232, -180)). (The corners are in the wrong order).
Methods
- contains(latlng)
- Returns true if the bounds contain the location
- containsBounds(latlngbounds)
- Returns true if it full contains the other bounds
- equals(latlngbounds)
- Returns true if the bounds are equal
- extend(latlng)
- Enlarges the bounds to be the smallest rectangle containing the previous area plus the specified location
- getNorthEast()
- Returns a GLatLng representing the NorthEast corner
- getSouthWest()
- Returns a GLatLng representing the SouthWest corner
- intersects(latlngbounds)
- Returns true if the two bounds intersect
- isEmpty()
- Returns true if the bounds specify a negative region (i.e. the corners are specified in the wrong order)
- isFullLat()
- Returns true if the latitude range covers the whole Earth from -90 to +90
- isFullLng()
- Returns true if the longitude range covers the whole Earth -180 to +180
- lat
- Returns an object with no externally accessible class. This object has two accessible properties .lo and .hi which contain the low and high values of the latitude range expressed in radians.
- lng
- Returns an object with no externally accessible class. This object has two accessible properties .lo and .hi which contain the low and high values of the longitude range expressed in radians.
- toSpan()
- Returns a GLatLng object which represents the width and height of the region.
- toString()
- Returns a string in the format ((33.123, -120.123), (45.123, -100.23399999999998)). The values may differ slightly from what you originally specified due to being converted to radians and back to degrees.
Using map.getBoundsZoomLevel()
One way to use this without risking getting things the wrong way round is to start with an empty GLatLngBounds by creating a GLatLngBounds with no parameters. Then .extend it to include each of your points. Then zoom to the value returned by map.getBoundsZoomLevel.
var bounds = new GLatLngBounds;
for (var i=0; i<markers.length; i++) {
bounds.extend(markers[i].point);
}
map.setZoom(map.getBoundsZoomLevel(bounds));
Setting the new map center to the center of the GLatLngBounds is accomplished like this:
map.setCenter(bounds.getCenter());
GLog
The GLog class supports writing log messages to a log window. This enables the most basic debugging method: the print statement in Maps API javascript code.
The first call to a GLog method opens a div (called the log window here) anchored to the lower left corner of the browser window.
At the top of the log window is a title bar with two action links. The clear link will erase all messages from the log windows. The close link will close the log window. The log window will not reopen after it has been closed.
The main part of the log window is a scrolling text area where messages and timestamps are displayed. The window scrolls to display the most recently written message. The text in this area is selectable, so it can be copied to save the log.
Version 1 of the API included a GLog object which contained empty, or stub methods, so it was not usable.
A big Thank You to the Google Maps API development team for including this code in version 2.
Methods
- write(text)
- writes a text message into the log window.
- writeUrl(url)
- writes a hyperlink to the URL into the log window. You can click, shift-click or ctrl-click the URL to open the link in this or another window or tab.
- writeHtml(html)
- writes a fragment of html text into the log window.
Examples
Here are a few javascript examples to illustrate using the GLog object.
GLog.write('enter the initPage() function');
GEvent.addListener(map, 'moveend',
function(){
GLog.write('moveend: ' + map.getCenter().toString())
});
GLog.writeUrl(window.location.href);
GLog.writeHtml("<b>HTML</b> message<br/>in two lines.");
See this example page Glog Example (API Version 2) for a more complete example.
