Leaflet/examples/geojson.html

283 lines
12 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html>
<head>
<title>Leaflet - a modern, lightweight JavaScript library for interactive maps by CloudMade - Leaflet on Mobile Example</title>
2012-02-14 23:02:29 +08:00
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8" />
2012-02-14 23:02:29 +08:00
<meta property="og:title" content="Leaflet — an open-source JavaScript library for interactive maps" />
<meta property="og:description" content="Leaflet is a modern, lightweight BSD-licensed JavaScript library for making tile-based interactive maps for both desktop and mobile web browsers, developed by CloudMade to form the core of its next generation JavaScript API." />
2012-02-14 23:02:29 +08:00
<link rel="icon" type="image/png" href="../docs/images/favicon.png" />
<!-- Blueprint -->
<link rel="stylesheet" href="../docs/css/blueprint/screen.css" media="screen, projection">
<link rel="stylesheet" href="../docs/css/blueprint/print.css" media="print">
<!--[if lt IE 8]><link rel="stylesheet" href="../docs/css/blueprint/ie.css" media="screen, projection"><![endif]-->
<link rel="stylesheet" href="../docs/css/screen.css" media="screen, projection" />
<script src="../docs/highlight/highlight.pack.js"></script>
<link rel="stylesheet" href="../docs/highlight/styles/github.css" />
<!-- Leaflet -->
<link rel="stylesheet" href="../dist/leaflet.css" />
<!--[if lte IE 8]><link rel="stylesheet" href="../dist/leaflet.ie.css" /><![endif]-->
<script src="../dist/leaflet.js"></script>
</head>
<body>
<div class="container">
<h1>Leaflet</h1>
<h3 class="alt">A Modern, Lightweight Open-Source JavaScript Library for Interactive Maps by <a href="http://cloudmade.com">CloudMade</a></h3>
2012-02-14 23:02:29 +08:00
<ul class="nav clearfix">
<li><a href="../index.html">Overview</a></li>
2012-02-14 23:02:29 +08:00
<li><a href="../features.html">Features</a></li>
<li><a href="../examples.html">Examples</a></li>
<li><a href="../reference.html">Documentation</a></li>
<li><a href="../download.html">Download</a></li>
<li><a class="twitter-link" href="http://twitter.com/LeafletJS">@LeafletJS</a></li>
2012-02-14 23:02:29 +08:00
<li><a class="github-link" href="http://github.com/CloudMade/Leaflet">GitHub Repo</a></li>
</ul>
2012-02-14 23:02:29 +08:00
<p><a href="../examples.html">&larr; Back to the list of examples</a></p>
<h3>Using GeoJSON with Leaflet</h3>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<p>GeoJSON is becoming a very popular data format among many GIS technologies and services — it's simple, lightweight, straightforward, and Leaflet is quite good at handling it. In this example, you'll learn how to create and interact with map vectors created from <a href="http://geojson.org/">GeoJSON</a> objects.</p>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<div id="map" style="height: 220px; margin-bottom: 18px"></div>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<script src="sample-geojson.js" type="text/javascript"></script>
<script>
var map = new L.Map('map');
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
var cloudmadeUrl = 'http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/22677/256/{z}/{x}/{y}.png',
cloudmadeAttribution = 'Map data &copy; 2011 OpenStreetMap contributors, Imagery &copy; 2011 CloudMade',
cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18, attribution: cloudmadeAttribution});
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
map.setView(new L.LatLng(39.74738794765598, -105.00002861022949), 13)
.addLayer(cloudmade);
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
var BaseballIcon = L.Icon.extend({
iconUrl: 'baseball-marker.png',
shadowUrl: null,
iconSize: new L.Point(32, 37),
shadowSize: null,
iconAnchor: new L.Point(14, 37),
popupAnchor: new L.Point(2, -32)
});
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
var geojsonLayer = new L.GeoJSON(null, {
pointToLayer: function (latlng){
return new L.CircleMarker(latlng, {
radius: 8,
fillColor: "#ff7800",
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8
});
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
}
});
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
var anotherGeojsonLayer = new L.GeoJSON(coorsField, {
pointToLayer: function (latlng){
return new L.Marker(latlng, {
icon: new BaseballIcon()
});
}
});
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
geojsonLayer.on("featureparse", function(e){
var popupContent = "<p>I started out as a GeoJSON " + e.geometryType + ", but now I'm a Leaflet vector!</p>";
if (e.properties && e.properties.popupContent){
popupContent += e.properties.popupContent;
}
e.layer.bindPopup(popupContent);
if (e.properties && e.properties.style){
e.layer.setStyle(e.properties.style);
}
});
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
anotherGeojsonLayer.on("featureparse", function (e){
var popupContent = "<p>I started out as a GeoJSON " + e.geometryType + ", but now I'm a Leaflet vector!</p>";
if (e.properties && e.properties.popupContent){
popupContent += e.properties.popupContent;
}
e.layer.bindPopup(popupContent);
});
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
map.addLayer(geojsonLayer).addLayer(anotherGeojsonLayer);
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
geojsonLayer.addGeoJSON(freeBus);
geojsonLayer.addGeoJSON(bicycleRental);
geojsonLayer.addGeoJSON(campus);
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
anotherGeojsonLayer.addGeoJSON(coorsField);
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
</script>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<p><a target="_blank" href="geojson-example.html">View example on a separate page &rarr;</a></p>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<h3>About GeoJSON</h3>
<p>According to <a href="http://geojson.org">http://geojson.org</a>:</p>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<blockquote>GeoJSON is a format for encoding a variety of geographic data structures. A GeoJSON object may represent a geometry, a feature, or a collection of features. GeoJSON supports the following geometry types: Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection. Features in GeoJSON contain a geometry object and additional properties, and a feature collection represents a list of features.</blockquote>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<p>Leaflet supports all of the GeoJSON types above but <a href="http://geojson.org/geojson-spec.html#feature-objects">Features</a> and <a href="http://geojson.org/geojson-spec.html#feature-collection-objects">FeatureCollections</a> work best as they allow you to describe features with a set of properties. We can even use these properties to style our Leaflet vectors. (<a target="_blank" href="geojson-example.html">see example</a>)</p>
2012-02-14 23:02:29 +08:00
2011-09-28 09:20:16 +08:00
<h3>The GeoJSON layer</h3>
2012-02-14 23:02:29 +08:00
2011-09-28 09:20:16 +08:00
<p>GeoJSON objects are added to the map through a <a href="http://leaflet.cloudmade.com/reference.html#geojson">GeoJSON layer</a>. To create a GeoJSON layer and add it to a map we can use the following code.</p>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<pre><code class="css">var geojsonLayer = new L.GeoJSON();
map.addLayer(geojsonLayer);</code></pre>
<p>This creates an empty GeoJSON layer that we can easily add vectors to with the <code>addGeoJSON</code> method.</p>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<pre><code>var geojsonFeature = {
"type": "Feature",
"properties": {
"name": "Coors Field",
"amenity": "Baseball Stadium",
"popupContent": "This is where the Rockies play!"
},
"geometry": {
"type": "Point",
"coordinates": [-104.99404, 39.75621]
}
};
geojsonLayer.addGeoJSON(geojsonFeature);</code></pre>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<p>We can also instantiate the GeoJSON layer with a GeoJSON object to show it immediately, without having to call <code>addGeoJSON</code>.</p>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<pre><code>var geojsonLayer = new L.GeoJSON(geojsonFeature);
map.addLayer(geojsonLayer);</code></pre>
<h3>Popups</h3>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<p>We can use popups to show information about these features when they are clicked. To accomplish this we'll listen to the <code>featureparse</code> event of the GeoJSON layer. Here we're checking to see if a feature has a property named "popupContent" and if so binding a popup to the feature so this text (or HTML) appears when clicked.</p>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<pre><code>geojsonLayer.on("featureparse", function (e) {
if (e.properties &amp;&amp; e.properties.popupContent){
e.layer.bindPopup(e.properties.popupContent);
}
});</code></pre>
<p>Make sure to do this <em>before</em> adding GeoJSON objects through the <code>addGeoJSON</code> method.</p>
2011-09-28 09:20:16 +08:00
<h3>Styling Features</h3>
2011-09-30 18:34:06 +08:00
<p>We can also listen to the <code>featureparse</code> event to style our Polyline and Polygon features. As with our popup content, we can store our styling information in the properties of the GeoJSON object as well. These style properties should match those found in <a href="http://leaflet.cloudmade.com/reference.html#path-options">path options</a>. Our feature with styling information might look something like:</p>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<pre><code>var freeBus = {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[-104.98726, 39.74136],
[-104.98720, 39.74132],
[-104.98715, 39.74127],
[-104.98713, 39.74117],
[-104.98712, 39.74106], ...
]
},
"properties": {
"name": "16th Street Free Bus",
"style": {
"color": "#004070",
"weight": 4,
"opacity": 0.9
},
"popupContent": "This is the 16th street free bus ..."
}
};</code></pre>
<p>To change the feature's style from the Leaflet default, we simply call the <code>setStyle</code> method on our layer (<code>e.layer</code>) in our <code>featureparse</code> event listener.</p>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<pre><code>geojsonLayer.on("featureparse", function (e){
if (e.properties &amp;&amp; e.properties.style &amp;&amp; e.layer.setStyle){
// The setStyle method isn't available for Points. More on that below ...
e.layer.setStyle(e.properties.style);
}
});</code></pre>
<h3>Styling Points</h3>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<p>Points are handled differently than polylines and polygons. By default simple markers are drawn for GeoJSON Points. We can alter this by passing a <code>pointToLayer</code> function in a <a href="http://leaflet.cloudmade.com/reference.html#geojson-options">GeoJSON options</a> object when creating the GeoJSON layer. This function is passed a <a href="http://leaflet.cloudmade.com/reference.html#latlng">LatLng</a> and should return an instance of ILayer, in this case likely a <a href="http://leaflet.cloudmade.com/reference.html#marker">Marker</a> or <a href="http://leaflet.cloudmade.com/reference.html#circlemarker">CircleMarker</a>.</p>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<p>Here we're using a CircleMarker:</p>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<pre><code>var geojsonMarkerOptions = {
radius: 8,
fillColor: "#ff7800",
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8
};
var geojsonLayer = new L.GeoJSON(someGeojsonFeature, {
pointToLayer: function (latlng) {
return new L.CircleMarker(latlng, geojsonMarkerOptions);
}
});</code></pre>
<p>And here's an example of using a custom Marker:</p>
2012-02-14 23:02:29 +08:00
2011-09-30 18:34:06 +08:00
<pre><code>var geojsonLayer = new L.GeoJSON(someGeojsonFeature, {
pointToLayer: function (latlng) {
return new L.Marker(latlng, {
icon: new MyCustomIcon()
});
}
});</code></pre>
<p>View the <a href="geojson-example.html">example page</a> to see in detail what is possible with the GeoJSON layer.</p>
2012-02-14 23:02:29 +08:00
<hr />
2012-02-14 23:02:29 +08:00
<p class="quiet">&copy; 2011 <a href="http://cloudmade.com">CloudMade</a>. Map data &copy; 2011 <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>.</p>
</div>
2012-02-14 23:02:29 +08:00
<a href="http://github.com/CloudMade/Leaflet"><img id="forkme" src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" /></a>
2012-02-14 23:02:29 +08:00
<script>
hljs.tabReplace = ' ';
hljs.initHighlightingOnLoad();
2012-02-14 23:02:29 +08:00
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push([ '_setAccount', 'UA-4147697-4' ]);
_gaq.push([ '_trackPageview' ]);
2011-09-29 22:30:48 +08:00
(function(){
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl'
: 'http://www')
+ '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
2012-02-21 23:14:40 +08:00
<script type="text/javascript">
var uvOptions = {};
(function() {
var uv = document.createElement('script'); uv.type = 'text/javascript'; uv.async = true;
uv.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'widget.uservoice.com/ygv5CFpu3yBQFTFPOAdFg.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(uv, s);
})();
</script>
</body>
2012-02-14 23:02:29 +08:00
</html>