Google Maps API Reference——Google 地图 API 文档之七-1

本文档介绍了 Google Maps API 的核心功能及使用方法,包括地图控制、覆盖物管理、信息窗口操作等。同时还提供了多种事件监听及坐标转换的方法,帮助开发者更好地实现地图应用。

Google Maps API Reference

The Google Maps API is now integrated with the Google AJAX API loader, which creates a common namespace for loading and using multiple Google AJAX APIs. This framework allows you to use the optional google.maps.* namespace for all classes, methods and properties you currently use in the Google Maps API, replacing the normal G prefix with this namespace. Don't worry: the existing G namespace will continue to be supported.


For example, the GMap2 object within the Google Maps API can also be defined as google.maps.Map2.


Note that this reference documentation refers only to the existing G namespace.

If you only want to use the map to display your content, then you need to know these classes, types, and functions:

If you want to extend the functionality of the maps API by implementing your own controls, overlays, or map types, then you also need to know these classes and types:

class GMap2

Instantiate class GMap2 in order to create a map. This is the central class in the API. Everything else is auxiliary.

Constructor

ConstructorDescription
GMap2(container, opts?)Creates a new map inside of the given HTML container, which is typically a DIV element. If no set of map types is given in the optional argument opts.mapTypes, the default set G_DEFAULT_MAP_TYPES is used. If no size is given in the optional argument opts.size, then the size of the container is used. If opts.size is given, then the container element of the map is resized accordingly. See class GMapOptions.

Methods

Configuration
MethodsReturn ValueDescription
enableDragging()noneEnables the dragging of the map (enabled by default).
disableDragging()noneDisables the dragging of the map.
draggingEnabled()BooleanReturns true iff the dragging of the map is enabled.
enableInfoWindow()noneEnables info window operations on the map (enabled by default).
disableInfoWindow()noneCloses the info window, if it is open, and disables the opening of a new info window.
infoWindowEnabled()BooleanReturns true iff the info window is enabled.
enableDoubleClickZoom()noneEnables double click to zoom in and out (disabled by default). (Since 2.58)
disableDoubleClickZoom()noneDisables double click to zoom in and out. (Since 2.58)
doubleClickZoomEnabled()BooleanReturns true iff double click to zoom is enabled. (Since 2.58)
enableContinuousZoom()noneEnables continuous smooth zooming for select browsers (disabled by default). (Since 2.58)
disableContinuousZoom()noneDisables continuous smooth zooming. (Since 2.58)
continuousZoomEnabled()BooleanReturns true if continuous smooth zooming is enabled. (Since 2.58)
enableScrollWheelZoom()noneEnables zooming using a mouse's scroll wheel. Note: scroll wheel zoom is disabled by default. (Since 2.78)
disableScrollWheelZoom()noneDisables zooming using a mouse's scroll wheel. Note: scroll wheel zoom is disabled by default. (Since 2.78)
scrollWheelZoomEnabled()BooleanReturns a Boolean indicating whether scroll wheel zooming is enabled. (Since 2.78)
Controls
MethodsReturn ValueDescription
addControl(control, position?)noneAdds the control to the map. The position on the map is determined by the optional position argument. If this argument is absent, the default position of the control is used, as determined by the GControl.getDefaultPosition() method. A control instance must not be added more than once to the map.
removeControl(control)noneRemoves the control from the map. It does nothing if the control was never added to the map.
getContainer()NodeReturns the DOM object that contains the map. Used by GControl.initialize().
Map Types
MethodsReturn ValueDescription
getMapTypes()Array of GMapTypeReturns the array of map types registered with this map.
getCurrentMapType()GMapTypeReturns the currently selected map type.
setMapType(type)noneSelects the given new map type. The type must be known to the map. See the constructor, and the method addMapType().
addMapType(type)noneAdds a new map type to the map. See section GMapType for how to define custom map types.
removeMapType(type)noneRemoves the map type from the map. Will update the set of buttons displayed by the GMapTypeControl and fire the removemaptype event.
Map State
MethodsReturn ValueDescription
isLoaded()BooleanReturns true iff the map was initialized by setCenter() since it was created.
getCenter()GLatLngReturns the geographical coordinates of the center point of the map view.
getBounds()GLatLngBoundsReturns the the visible rectangular region of the map view in geographical coordinates.
getBoundsZoomLevel(bounds)NumberReturns the zoom level at which the given rectangular region fits in the map view. The zoom level is computed for the currently selected map type. If no map type is selected yet, the first on the list of map types is used.
getSize()GSizeReturns the size of the map view in pixels.
getZoom()NumberReturns the current zoom level.
Modify the Map State
MethodsReturn ValueDescription
setCenter(center, zoom?, type?)noneSets the map view to the given center. Optionally, also sets zoom level and map type. The map type must be known to the map. See the constructor, and the method addMapType(). This method must be called first after construction to set the initial state of the map. It is an error to call other operations on the map after construction.
panTo(center)noneChanges the center point of the map to the given point. If the point is already visible in the current map view, change the center in a smooth animation.
panBy(distance)noneStarts a pan animation by the given distance in pixels.
panDirection(dx, dy)noneStarts a pan animation by half the width of the map in the indicated directions. +1 is right and down, -1 is left and up, respectively.
setZoom(level)noneSets the zoom level to the given new value.
zoomIn()noneIncrements zoom level by one.
zoomOut()noneDecrements zoom level by one.
savePosition()noneStores the current map position and zoom level for later recall by returnToSavedPosition().
returnToSavedPosition()noneRestores the map view that was saved by savePosition().
checkResize()noneNotifies the map of a change of the size of its container. Call this method after the size of the container DOM object has changed, so that the map can adjust itself to fit the new size.
Overlays
MethodsReturn ValueDescription
addOverlay(overlay)noneAdds an overlay to the map and fires the addoverlay event.
removeOverlay(overlay)noneRemoves the overlay from the map. If the overlay was on the map, it fires the removeoverlay event.
clearOverlays()noneRemoves all overlay from the map, and fires the clearoverlays event.
getPane(pane)NodeReturns a DIV that holds the object in the layer identified by pane. Used by GOverlay instances in method GOverlay.initialize() instances to draw themselves on the map
Info Window
MethodsReturn ValueDescription
openInfoWindow(point, node, opts?)noneOpens a simple info window at the given point. Pans the map such that the opened info window is fully visible. The content of the info window is given as a DOM node.
openInfoWindowHtml(point, html, opts?)noneOpens a simple info window at the given point. Pans the map such that the opened info window is fully visible. The content of the info window is given as HTML text.
openInfoWindowTabs(point, tabs, opts?)noneOpens a tabbed info window at the given point. Pans the map such that the opened info window is fully visible. The content of the info window is given as DOM nodes.
openInfoWindowTabsHtml(point, tabs, opts?)noneOpens a tabbed info window at the given point. Pans the map such that the opened info window is fully visible. The content of the info window is given as HTML text.
showMapBlowup(point, opts?)noneOpens an info window at the given point that contains a closeup view on the map around this point.
updateInfoWindow(tabs, onupdate?)noneUpdates the content of the currently open GInfoWindow object, without repositioning. The info window is resized to fit the new content. The optional onupdate callback function is called after the info window content is actually changed. (Since 2.85)
updateCurrentTab(modifier, onupdate?)noneUpdates the currently selected tab, causing a resize of the GInfoWindow object, without repositioning. The modifier function is used to modify the currently selected tab and is passed a GInfoWindowTab as an argument. The optional onupdate callback function is called after the info window displays the new content. (Since 2.85)
closeInfoWindow()noneCloses the currently open info window.
getInfoWindow()GInfoWindowReturns the info window object of this map. If no info window exists yet, it is created, but not displayed. This operation is not influenced by enableInfoWindow().
Coordinate Transformations
MethodsReturn ValueDescription
fromLatLngToDivPixel(latlng)GPointComputes the pixel coordinates of the given geographical point in the DOM element that holds the draggable map. You need this method to position a custom overlay when you implement the GOverlay.redraw() method for a custom overlay.
fromDivPixelToLatLng(pixel)GLatLngComputes the geographical coordinates from pixel coordinates in the div that holds the draggable map. You need this when you implement interaction with custom overlays.
fromContainerPixelToLatLng(pixel)GLatLngComputes the geographical coordinates of the point at the given pixel coordinates in the DOM element that contains the map on the page. You need this when you implement interaction of custom controls with the map.

Events

EventsArgumentsDescription
addmaptypetypeThis event is fired when a map type is added to the map.
removemaptypetypeThis event is fired when a map type is removed from the map.
click overlay, point This event is fired when the map is clicked with the mouse. If the click was on a marker, then the marker is passed to the event handler in the overlay argument, and a click event is also fired on the marker. Otherwise, the geographical coordinates of the point that was clicked are passed in the point argument.
dblclick overlay, point This event is fired when a double click is done on the map. Notice that this event will not be fired if the double click was on a marker. The geographical coordinates of the point that was double clicked are passed in the point argument. The overlay argument is always set to null. (Since 2.83)
singlerightclick point, src, overlay? This event is fired when the DOM contextmenu event is fired on the map container. If the right click was on a marker, then the marker is passed to the event handler in the overlay argument. The pixel coordinates (in the DOM element that holds the map) of the point that was right clicked and the source element of the DOM event are passed in the point and src arguments respectively. Note that if it is a double right click and double click to zoom is enabled, then the map zooms out and no singlerightclick event is fired. If, however, double click to zoom is disabled, two singlerightclick events will be fired. (Since 2.84)
movestartnoneThis event is fired when the map view starts changing. This can be caused by dragging, in which case a dragstart event is also fired, or by invocation of a method that changes the map view.
movenoneThis event is fired, possibly repeatedly, while the map view is changing.
moveendnoneThis event is fired when the change of the map view ends.
zoomend oldLevel, newLevel This event is fired when the map reaches a new zoom level. The event handler receives the previous and the new zoom level as arguments.
maptypechangednoneThis event is fired when another map type is selected.
infowindowopennoneThis event is fired when the info window opens.
infowindowbeforeclosenoneThis event is fired before the info window closes. (Since 2.83)
infowindowclosenoneThis event is fired when the info window closes. The event infowindowbeforeclose is fired before this event. If a currently open info window is reopened at a different point using another call to openInfoWindow*(), the events infowindowbeforeclose, infowindowclose and infowindowopen are fired in this order.
addoverlayoverlayThis event is fired when a single overlay is added to the map by the method addOverlay(). The new overlay is passed as an argument overlay to the event handler.
removeoverlayoverlayThis event is fired when a single overlay is removed by the method removeOverlay(). The overlay that was removed is passed as an argument overlay to the event handler.
clearoverlaysnoneThis event is fired when all overlays are removed at once by the method clearOverlays().
mouseoverlatlngThis event is fired when the user moves the mouse over the map from outside the map.
mouseoutlatlngThis event is fired when the user moves the mouse off the map.
mousemovelatlngThis event is fired when the user moves the mouse inside the map.
dragstartnoneThis event is fired when the user starts dragging the map.
dragnoneThis event is repeatedly fired while the user drags the map.
dragendnoneThis event is fired when the user stops dragging the map.
loadnoneThis event is fired when the map setup is complete, and isLoaded() would return true. This means position, zoom, and map type are all initialized, but tile images may still be loading. (Since 2.52)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值