Leaflet

A Modern, Lightweight Open-Source JavaScript Library for Interactive Maps by CloudMade



Contents

UI layers

Raster layers

Vector layers

Utility

DOM utility

Mixins

L.Map

The central class of the API — it is used to create a map on a page and manipulate it.

Usage example

// initialize the map on the "map" div with a given center and zoom 
var map = new L.Map('map', {
	center: new L.LatLng(51.505, -0.09), 
	zoom: 13
});

// create a CloudMade tile layer
var cloudmadeUrl = 'http://{s}.tile.cloudmade.com/YOUR-API-KEY/997/256/{z}/{x}/{y}.png',
	cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18});

// add the CloudMade layer to the map
map.addLayer(cloudmade);

Constructor

Constructor Description
L.Map( <HTMLElement|String> id, <Map optionsoptions? ) Instantiates a map object given a div element (or its id) and optionally an object literal with map options described below.

Options

Option Type Default value Description
center LatLng null Initial geographical center of the map.
zoom Number null Initial map zoom.
layers ILayer[] [] Layers that will be added to the map initially.
dragging Boolean true Whether the map be draggable with mouse/touch or not.
touchZoom Boolean true Whether the map can be zoomed by touch-dragging with two fingers.
scrollWheelZoom Boolean true Whether the map can be zoomed by using the mouse wheel.
doubleClickZoom Boolean true Whether the map can be zoomed in by double clicking on it.
trackResize Boolean true Whether the map automatically handles browser window resize to update itself.

Methods that modify map state

Method Returns Description
setView( <LatLng> center, <Number> zoom, <Boolean> forceReset? ) this Sets the view of the map (geographical center and zoom). If forceReset is set to true, the map is reloaded even if it's eligible for pan animation (false by default).
setZoom( <Number> zoom ) this Sets the zoom of the map.
zoomIn() this Increases the zoom of the map by 1.
zoomOut() this Decreases the zoom of the map by 1.
fitBounds( <LatLngBounds> bounds ) this Sets a map view that contains the given geographical bounds with the maximum zoom level possible.
panTo( <LatLng> latlng ) this Pans the map to a given center. Makes an animated pan if new center is not more than one screen away from the current one.
panBy( <Point> point ) this Pans the map by a given number of pixels (animated).
addLayer( <ILayer> layer ) this Adds a given layer to the map.
removeLayer( <ILayer> layer ) this Removes the given layer from the map.
invalidateSize() this Checks if the map container size changed and updates the map if so — call it after you've changed the map size dynamically.

Methods that get map state

Method Returns Description
getCenter() LatLng Returns the geographical center of the map view.
getZoom() Number Returns the current zoom of the map view.
getMinZoom() Number Returns the minimum zoom level of the map.
getMaxZoom() Number Returns the maximum zoom level of the map.
getBoundsZoom( <LatLngBounds> bounds ) Number Returns the maximum zoom level on which the given bounds fit to the map view in its entirety.
getSize() Point Returns the current size of the map container.
getPixelBounds() Bounds Returns the bounds of the current map view in projected pixel coordinates (sometimes useful in layer and overlay implementations).
getPixelOrigin() Point Returns the projected pixel coordinates of the top left point of the map layer (useful in custom layer and overlay implementations).
getPanes() Panes Returns an object for accessing different panes of the map (tile pane, marker pane, etc.).

Conversion methods

Method Returns Description
latLngToLayerPoint( <LatLnglatlng ) Point Returns the map layer point that corresponds to the given geographical coordinates (useful for placing overlays on the map).
layerPointToLatLng( <Pointpoint ) LatLng Returns the geographical coordinates of a given map layer point.
mouseEventToContainerPoint( <MouseEvent> event ) Point Returns the pixel coordinates of a mouse click (relative to the top left corner of the map) given its event object.
mouseEventToLayerPoint( <MouseEvent> event ) Point Returns the pixel coordinates of a mouse click relative to the map layer given its event object.
mouseEventToLatLng( <MouseEvent> event ) LatLng Returns the geographical coordinates of the point the mouse clicked on given the click's event object.
containerPointToLayerPoint( <Pointpoint ) Point Converts the point relative to the map container to a point relative to the map layer.
layerPointToContainerPoint( <Pointpoint ) Point Converts the point relative to the map layer to a point relative to the map container.
project( <LatLnglatlng, <Number> zoom? ) Point Projects the given geographical coordinates to pixel coordinates for the given zoom level (current zoom level by default).
unproject( <Pointpoint, <Number> zoom? ) LatLng Projects the given pixel coordinates to geographical coordinates for the given zoom level (current zoom level by default).

L.Path

An abstract class that contains options and constants shared between vector overlays (Polygon, Polyline, Circle). Do not use it directly.

Options

Option Type Default value Description
stroke Boolean true Whether to draw stroke along the path. Set it to false to disable borders on polygons or circles.
color String '#03f' Stroke color.
weight Number 5 Stroke width in pixels.
opacity Number 0.5 Stroke opacity.
fill Boolean depends Whether to fill the path with color. Set it to false to disable filling on polygons or circles.
fillColor String same as color Fill color.
fillOpacity Number 0.2 Fill opacity.

Constants

Constant Type Value Description
L.Path.SVG Boolean depends True if SVG is used for vector rendering (true for most modern browsers).
L.Path.VML Boolean depends True if VML is used for vector rendering (IE 6-8).
L.Path.CLIP_PADDING Number 0.5 for SVG
0.02 for VML
How much to extend the clip area around the map view (relative to its size, e.g. 0.5 is half the screen in each direction). Smaller values mean that you will see clipped ends of paths while you're dragging the map, and bigger values decrease drawing performance.

Polyline

A class for drawing polyline overlays on a map. Extends Path. Use Map#addLayer to add it to the map.

Usage example

// create a red polyline from an arrays of LatLng points
var polyline = new L.Polyline(latlngs, {color: 'red'});

// zoom the map to the polyline
map.fitBounds(new L.LatLngBounds(latlngs));

// add the polyline to the map
map.addLayer(polyline);

Constructor

Constructor Description
L.Polyline( <LatLng[]> latlngs, <Polyline optionsoptions? ) Instantiates a polyline object given an array of geographical points and optionally an options object.

Options

You can use Path options and additionally the following options:

Option Type Default value Description
smoothFactor Number 1.0 How much to simplify the polyline on each zoom level. More means better performance and smoother look, and less means more accurate representation.
noClip Boolean false Disabled polyline clipping.

Polygon

A class for drawing polygon overlays on a map. Extends Polyline. Use Map#addLayer to add it to the map.

Constructor

Constructor Description
L.Polygon( <LatLng[]> latlngs, <Polyline optionsoptions? ) Instantiates a polygon object given an array of geographical points and optionally an options object (the same as for Polyline).

Circle

A class for drawing circle overlays on a map. Extends Path. Use Map#addLayer to add it to the map.

Constructor

Constructor Description
L.Circle( <LatLnglatlng, <Number> radius, <Path optionsoptions? ) Instantiates a polygon object given an geographical point, a radius in pixels and optionally an options object.

L.Point

Represents a point with x and y coordinates in pixels.

Constructor

Constructor Description
L.Point( <Number> x, <Number> y ) Creates a Point object with the given x and y coordinates.

Properties

Property Type Description
x Number The x coordinate.
y Number The y coordinate.

Methods

Method Returns Description
add( <PointotherPoint ) Point Returns the result of addition of the current and the given points.
subtract( <PointotherPoint ) Point Returns the result of subtraction of the given point from the current.
multiplyBy( <Number> number ) Point Returns the result of multiplication of the current point by the given number.
divideBy( <Number> number ) Point Returns the result of division of the current point by the given number.
distanceTo( <PointotherPoint ) Number Returns the distance between the current and the given points.
clone() Point Returns a copy of the current point.
round() Point Returns a copy of the current point with rounded coordinates.
toString() String Returns a string representation of the point for debugging purposes.

L.LatLng

Represents a geographical point with a certain latitude and longitude.

Constructor

Constructor Description
L.LatLng( <Number> latitude, <Number> longitude, <Boolean> noWrap? ) Creates a LatLng object with the given latitude and longitude. Wraps longitude to lie between -180 and 180 and clamps longitude between -90 and 90 by default — you can disable this with the noWrap argument.

Properties

Property Type Description
lat Number Latitude in degrees.
lng Number Longitude in degrees.

Methods

Method Returns Description
equals( <LatLngotherLatlng ) Boolean Returns true if the given LatLng point is at the same position (within a small margin of error).
toString() String Returns a string representation of the point (for debugging purposes).

Constants

Constant Type Value Description
DEG_TO_RAD Number Math.PI / 180 A multiplier for converting degrees into radians.
RAD_TO_DEG Number 180 / Math.PI A multiplier for converting radians into degrees.
MAX_MARGIN Number 1.0E-9 Max margin of error for the equality check.

© 2011 CloudMade. Map data © 2011 OpenStreetMap contributors, CC-BY-SA.

Fork me on GitHub