3 L.OSM.TileLayer = L.TileLayer.extend({
5 url: 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
6 attribution: '© <a target="_parent" href="http://www.openstreetmap.org">OpenStreetMap</a> and contributors, under an <a target="_parent" href="http://www.openstreetmap.org/copyright">open license</a>'
9 initialize: function (options) {
10 options = L.Util.setOptions(this, options);
11 L.TileLayer.prototype.initialize.call(this, options.url);
15 L.OSM.Mapnik = L.OSM.TileLayer.extend({
17 url: 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
22 L.OSM.CycleMap = L.OSM.TileLayer.extend({
24 url: 'http://{s}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png'
28 L.OSM.TransportMap = L.OSM.TileLayer.extend({
30 url: 'http://{s}.tile2.opencyclemap.org/transport/{z}/{x}/{y}.png'
34 L.OSM.MapQuestOpen = L.OSM.TileLayer.extend({
36 url: 'http://otile{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png',
38 attribution: "Tiles courtesy of <a href='http://www.mapquest.com/' target='_blank'>MapQuest</a> <img src='http://developer.mapquest.com/content/osm/mq_logo.png'>"
42 L.OSM.DataLayer = L.FeatureGroup.extend({
44 areaTags: ['area', 'building', 'leisure', 'tourism', 'ruins', 'historic', 'landuse', 'military', 'natural', 'sport'],
45 uninterestingTags: ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
49 initialize: function (xml, options) {
50 L.Util.setOptions(this, options);
52 L.FeatureGroup.prototype.initialize.call(this);
59 addData: function (features) {
60 if (!(features instanceof Array)) {
61 features = this.buildFeatures(features);
64 for (var i = 0; i < features.length; i++) {
65 var feature = features[i], layer;
67 if (feature.type === "node") {
68 layer = L.circleMarker(feature.latLng, this.options.styles.node);
70 var latLngs = new Array(feature.nodes.length);
72 for (var j = 0; j < feature.nodes.length; j++) {
73 latLngs[j] = feature.nodes[j].latLng;
76 if (this.isWayArea(feature)) {
77 latLngs.pop(); // Remove last == first.
78 layer = L.polygon(latLngs, this.options.styles.area);
80 layer = L.polyline(latLngs, this.options.styles.way);
85 layer.feature = feature;
89 buildFeatures: function (xml) {
91 nodes = L.OSM.getNodes(xml),
92 ways = L.OSM.getWays(xml, nodes),
93 relations = L.OSM.getRelations(xml, nodes, ways);
95 for (var node_id in nodes) {
96 var node = nodes[node_id];
97 if (this.interestingNode(node, ways, relations)) {
102 for (var i = 0; i < ways.length; i++) {
110 isWayArea: function (way) {
111 if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
115 for (var key in way.tags) {
116 if (~this.options.areaTags.indexOf(key)) {
124 interestingNode: function (node, ways, relations) {
127 for (var i = 0; i < ways.length; i++) {
128 if (ways[i].nodes.indexOf(node) >= 0) {
138 for (var i = 0; i < relations.length; i++) {
139 if (relations[i].members.indexOf(node) >= 0)
143 for (var key in node.tags) {
144 if (this.options.uninterestingTags.indexOf(key) < 0) {
153 L.Util.extend(L.OSM, {
154 getNodes: function (xml) {
157 var nodes = xml.getElementsByTagName("node");
158 for (var i = 0; i < nodes.length; i++) {
159 var node = nodes[i], id = node.getAttribute("id");
163 latLng: L.latLng(node.getAttribute("lat"),
164 node.getAttribute("lon"),
166 tags: this.getTags(node)
173 getWays: function (xml, nodes) {
176 var ways = xml.getElementsByTagName("way");
177 for (var i = 0; i < ways.length; i++) {
178 var way = ways[i], nds = way.getElementsByTagName("nd");
181 id: way.getAttribute("id"),
183 nodes: new Array(nds.length),
184 tags: this.getTags(way)
187 for (var j = 0; j < nds.length; j++) {
188 way_object.nodes[j] = nodes[nds[j].getAttribute("ref")];
191 result.push(way_object);
197 getRelations: function (xml, nodes, ways) {
200 var rels = xml.getElementsByTagName("relation");
201 for (var i = 0; i < rels.length; i++) {
202 var rel = rels[i], members = rel.getElementsByTagName("member");
205 id: rel.getAttribute("id"),
207 members: new Array(members.length),
208 tags: this.getTags(rel)
211 for (var j = 0; j < members.length; j++) {
212 if (members[j].getAttribute("type") === "node")
213 rel_object.members[j] = nodes[members[j].getAttribute("ref")];
214 else // relation-way and relation-relation membership not implemented
215 rel_object.members[j] = null;
218 result.push(rel_object);
224 getTags: function (xml) {
227 var tags = xml.getElementsByTagName("tag");
228 for (var j = 0; j < tags.length; j++) {
229 result[tags[j].getAttribute("k")] = tags[j].getAttribute("v");