diff --git a/_layouts/default.html b/_layouts/default.html index 69a6eea2..9b3ca054 100644 --- a/_layouts/default.html +++ b/_layouts/default.html @@ -35,6 +35,14 @@ + +
diff --git a/docs/images/leaf-shadow.png b/docs/images/leaf-shadow.png index f8129861..119a71db 100644 Binary files a/docs/images/leaf-shadow.png and b/docs/images/leaf-shadow.png differ diff --git a/examples.md b/examples.md index 7a80c358..20cee828 100644 --- a/examples.md +++ b/examples.md @@ -5,7 +5,7 @@ title: Tutorials ## Leaflet Tutorials -Every example here comes with step-by-step code explanation and is easy enough even for beginner JavaScript developers. +Every tutorial here comes with step-by-step code explanation and is easy enough even for beginner JavaScript developers. *** [][1] @@ -19,21 +19,21 @@ A simple step-by-step guide that will quickly get you started with Leaflet basic ### [Leaflet on Mobile][2] -In this example, you'll learn how to create a fullscreen map tuned for mobile devices like iPhone, iPad or Android phones, and how to easily detect and use the current user location. +In this tutorial, you'll learn how to create a fullscreen map tuned for mobile devices like iPhone, iPad or Android phones, and how to easily detect and use the current user location. *** [][3] ### [Markers With Custom Icons][3] -In this pretty example, you'll learn how to easily define your own icons for use by the markers you put on the map. +In this pretty tutorial, you'll learn how to easily define your own icons for use by the markers you put on the map. *** [][4] ### [Using GeoJSON with Leaflet][4] -In this example, you'll learn how to create and interact with map vectors created from [GeoJSON][5] objects. +In this tutorial, you'll learn how to create and interact with map vectors created from [GeoJSON][5] objects. *** [][6] diff --git a/examples/custom-icons-example.html b/examples/custom-icons-example.html index cb6c521a..4263e2fa 100644 --- a/examples/custom-icons-example.html +++ b/examples/custom-icons-example.html @@ -23,16 +23,16 @@ var LeafIcon = L.Icon.extend({ options: { - iconUrl: '../docs/images/leaf-green.png', shadowUrl: '../docs/images/leaf-shadow.png', - iconSize: [38, 95], - shadowSize: [68, 95], - iconAnchor: [22, 94], - popupAnchor: [-3, -76] + iconSize: [38, 95], + shadowSize: [50, 64], + iconAnchor: [22, 94], + shadowAnchor: [4, 62], + popupAnchor: [-3, -76] } }); - var greenIcon = new LeafIcon(), + var greenIcon = new LeafIcon({iconUrl: '../docs/images/leaf-green.png'}), redIcon = new LeafIcon({iconUrl: '../docs/images/leaf-red.png'}), orangeIcon = new LeafIcon({iconUrl: '../docs/images/leaf-orange.png'}); diff --git a/examples/custom-icons.md b/examples/custom-icons.md index 7d95ef38..4928840e 100644 --- a/examples/custom-icons.md +++ b/examples/custom-icons.md @@ -5,7 +5,7 @@ title: Markers With Custom Icons ### Markers With Custom Icons -In this example, you'll learn how to easily define your own icons for use by the markers you put on the map. +In this tutorial, you'll learn how to easily define your own icons for use by the markers you put on the map.
@@ -14,7 +14,7 @@ In this example, you'll learn how to easily define your own icons for use by the ### Preparing the images -To make a custom icon, we usually need two images --- the actual icon image and the image of its shadow. For this example, we took the Leaflet logo and created four images out of it --- 3 leaf images of different colors and one shadow image for the three: +To make a custom icon, we usually need two images --- the actual icon image and the image of its shadow. For this tutorial, we took the Leaflet logo and created four images out of it --- 3 leaf images of different colors and one shadow image for the three:

@@ -23,71 +23,78 @@ To make a custom icon, we usually need two images --- the actual icon image and

-The white area in the images is actually transparent, and they are saved in the PNG24 format. Notice there's some excessive area on the left of the shadow image — its cropped in such way that if you align the icon and the shadow images on top of each other, the top left corners are in the same spot. +Note that the white area in the images is actually transparent. +### Creating an icon + +Marker icons in Leaflet are defined by [L.Icon](../reference.html#icon) objects, which are passed as an option when creating markers. Let's create a green leaf icon: + + var greenIcon = L.icon({ + iconUrl: 'leaf-green.png', + shadowUrl: 'leaf-shadow.png', + + iconSize: [38, 95], // size of the icon + shadowSize: [50, 64], // size of the shadow + iconAnchor: [22, 94], // point of the icon which will correspond to marker's location + shadowAnchor: [4, 62], // the same for the shadow + popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor + }); + +Now putting a marker with this icon on a map is easy: + + L.marker([51.5, -0.09], {icon: greenIcon}).addTo(map); + +
### Defining an icon class -The default marker icons in Leaflet are defined by the [L.Icon](../reference.html#icon) class. Its instance (`new L.Icon()`) is then set by default in the `L.Marker` options. So how do we define our own icon class? Inherit from `L.Icon`! It's really easy in Leaflet: +What if we need to create several icons that have lots in common? Lets define our own icon class containing the shared options, inheriting from `L.Icon`! It's really easy in Leaflet: var LeafIcon = L.Icon.extend({ options: { - iconUrl: '../docs/images/leaf-green.png', - shadowUrl: '../docs/images/leaf-shadow.png', - iconSize: new L.Point(38, 95), - shadowSize: new L.Point(68, 95), - iconAnchor: new L.Point(22, 94), - popupAnchor: new L.Point(-3, -76) + shadowUrl: 'leaf-shadow.png', + iconSize: [38, 95], + shadowSize: [50, 64], + iconAnchor: [22, 94], + shadowAnchor: [4, 62], + popupAnchor: [-3, -76] } }); -Now we've defined a green leaf icon class. The iconSize and shadowSize properties are the sizes of the corresponding images in pixels, the iconAnchor property is the coordinates of the "tip" of our icon, and the popupAnchor property points to a point from which a marker popup would open relative to the iconAnchor point. +Now we can create all three of our leaf icons from this class and use them: + var greenIcon = new LeafIcon({iconUrl: 'leaf-green.png'}), + redIcon = new LeafIcon({iconUrl: 'leaf-red.png'}), + orangeIcon = new LeafIcon({iconUrl: 'leaf-orange.png'}); -### Using icons in markers +You may have noticed that we used the `new` keyword for creating LeafIcon instances. So why do all Leaflet classes get created without it? The answer is simple: the real Leaflet classes are named with a capital letter (e.g. `L.Icon`), and they also need to be created with `new`, but there are also shortcuts with lowercase names (`L.icon`), created for convenience like this: -To use our newly created icon class in a marker, we need to create an instance of that class and pass it to the marker constructor in the options: + L.icon = function (options) { + return new L.Icon(options); + }; - var greenIcon = new LeafIcon(), - marker = new L.Marker(new L.LatLng(51.5, -0.09), {icon: greenIcon}); +You can do the same with your classes too. OK, lets finally put some markers with these icons on the map: -Awesome, it works! But what about the orange and the red ones? You could repeat the whole process above for each of them, but there's a much easier way. Notice that these icons would have the same properties except for one --- the iconUrl. Because it's a frequent case, the L.Icon constructor has an optional argument --- an iconUrl to replace the defined one. Let's see: + L.marker([51.5, -0.09], {icon: greenIcon}).addTo(map).bindPopup("I am a green leaf."); + L.marker([51.495, -0.083], {icon: redIcon}).addTo(map).bindPopup("I am a red leaf."); + L.marker([51.49, -0.1], {icon: orangeIcon}).addTo(map).bindPopup("I am an orange leaf."); - var greenIcon = new LeafIcon(), - redIcon = new LeafIcon({iconUrl: '../docs/images/leaf-red.png'}), - orangeIcon = new LeafIcon({iconUrl: '../docs/images/leaf-orange.png'}); - -Now we can use all the three icons: - - var marker1 = new L.Marker(new L.LatLng(51.5, -0.09), {icon: greenIcon}), - marker2 = new L.Marker(new L.LatLng(51.495, -0.083), {icon: redIcon}), - marker3 = new L.Marker(new L.LatLng(51.49, -0.1), {icon: orangeIcon}); - -marker1.bindPopup("I am a green leaf."); -marker2.bindPopup("I am a red leaf."); -marker3.bindPopup("I am an orange leaf."); - -map.addLayer(marker1).addLayer(marker2).addLayer(marker3); - -That's all. Now take a look at the full example, the L.Icon docs, or browse other examples. +That's it. Now take a look at the [full example](custom-icons-example.html), the [L.Icon docs](../reference.html#icon), or browse [other examples](../examples.html). diff --git a/examples/mobile.html b/examples/mobile.html deleted file mode 100644 index 3dcf96c7..00000000 --- a/examples/mobile.html +++ /dev/null @@ -1,72 +0,0 @@ ---- -layout: tutorial -title: Leaflet on Mobile ---- - -

Leaflet on Mobile

- -

In this example, you'll learn how to create a fullscreen map tuned for mobile devices like iPhone, iPad or Android phones, and how to easily detect and use the current user location.

- -

View example in fullscreen →

- -

Preparing the page

- -

First we'll take a look at the HTML & CSS code of the page. To make our map div element stretch to all available space (fullscreen), we can use the following CSS code:

- -
body {
-	padding: 0;
-	margin: 0;
-}
-html, body, #map {
-	height: 100%;
-}
- -

Also, we need to tell the mobile browser to disable unwanted scaling of the page and set it to its actual size by placing the following line in the head section or our HTML page:

- -
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
- -

Initializing the map

- -

We'll now initialize the map in the JavaScript code exactly like we did in the quick start guide, but won't set the map view yet:

- -
var map = L.map('map');
-
-L.tileLayer('http://{s}.tile.cloudmade.com/API-key/997/256/{z}/{x}/{y}.png', {
-	attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>',
-	maxZoom: 18
-}).addTo(map);
- -

Geolocation

- -

Leaflet has a very handy shortcut for zooming the map view to the detected location — locate method with the setView option, replacing the usual setView method in the code:

- -
map.locate({setView: true, maxZoom: 16});
- -

Here we specify 16 as the maximum zoom when setting the map view automatically. As soon as the user agrees to share its location and it's detected by the browser, the map will set the view to it. Now we have a working fullscreen mobile map! But what if we need to do something after the geolocation completed? Here's what the locationfound and locationerror events are for. Let's for example add a marker in the detected location, showing accuracy in a popup, by adding an event listener to locationfound event before the locateAndSetView call:

- -
function onLocationFound(e) {
-	var radius = e.accuracy / 2;
-
-	L.marker(e.latlng).addTo(map)
-		.bindPopup("You are within " + radius + " meters from this point").openPopup();
-
-	L.circle(e.latlng, radius).addTo(map);
-}
-
-map.on('locationfound', onLocationFound);
-
- -

Excellent! But it would also be nice to show an error message if the geolocation failed:

- -
function onLocationError(e) {
-	alert(e.message);
-}
-
-map.on('locationerror', onLocationError);
-
- -

If you have setView option set to true and the geolocation failed, it will set the view to the whole world.

- -

Now the example is complete — try it on your mobile phone: View the full example →

- -

Next steps would be to take a look at the detailed documentation and browse other examples.

diff --git a/examples/quick-start.md b/examples/quick-start.md index be99b377..c30abe7c 100644 --- a/examples/quick-start.md +++ b/examples/quick-start.md @@ -142,13 +142,9 @@ Now you've learned Leaflet basics and can start building map apps straight away!