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'
21 L.OSM.CycleMap = L.OSM.TileLayer.extend({
23 url: 'http://{s}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png'
27 L.OSM.TransportMap = L.OSM.TileLayer.extend({
29 url: 'http://{s}.tile2.opencyclemap.org/transport/{z}/{x}/{y}.png'
33 L.OSM.MapQuestOpen = L.OSM.TileLayer.extend({
35 url: 'http://otile{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png',
37 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'>"
41 L.OSM.DataLayer = L.FeatureGroup.extend({
43 areaTags: ['area', 'building', 'leisure', 'tourism', 'ruins', 'historic', 'landuse', 'military', 'natural', 'sport'],
44 uninterestingTags: ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
48 initialize: function (xml, options) {
49 L.Util.setOptions(this, options);
51 L.FeatureGroup.prototype.initialize.call(this);
58 addData: function (features) {
59 if (!(features instanceof Array)) {
60 features = this.buildFeatures(features);
63 for (var i = 0; i < features.length; i++) {
64 var feature = features[i], layer;
66 if (feature.type === "node") {
67 layer = L.circleMarker(feature.latLng, this.options.styles.node);
69 var latLngs = new Array(feature.nodes.length);
71 for (var j = 0; j < feature.nodes.length; j++) {
72 latLngs[j] = feature.nodes[j].latLng;
75 if (this.isWayArea(feature)) {
76 latLngs.pop(); // Remove last == first.
77 layer = L.polygon(latLngs, this.options.styles.area);
79 layer = L.polyline(latLngs, this.options.styles.way);
84 layer.feature = feature;
88 buildFeatures: function (xml) {
90 nodes = L.OSM.getNodes(xml),
91 ways = L.OSM.getWays(xml, nodes),
92 relations = L.OSM.getRelations(xml, nodes, ways);
94 for (var node_id in nodes) {
95 var node = nodes[node_id];
96 if (this.interestingNode(node, ways, relations)) {
101 for (var i = 0; i < ways.length; i++) {
109 isWayArea: function (way) {
110 if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
114 for (var key in way.tags) {
115 if (~this.options.areaTags.indexOf(key)) {
123 interestingNode: function (node, ways, relations) {
126 for (var i = 0; i < ways.length; i++) {
127 if (ways[i].nodes.indexOf(node) >= 0) {
137 for (var i = 0; i < relations.length; i++) {
138 if (relations[i].members.indexOf(node) >= 0)
142 for (var key in node.tags) {
143 if (this.options.uninterestingTags.indexOf(key) < 0) {
152 L.Util.extend(L.OSM, {
153 getNodes: function (xml) {
156 var nodes = xml.getElementsByTagName("node");
157 for (var i = 0; i < nodes.length; i++) {
158 var node = nodes[i], id = node.getAttribute("id");
162 latLng: L.latLng(node.getAttribute("lat"),
163 node.getAttribute("lon"),
165 tags: this.getTags(node)
172 getWays: function (xml, nodes) {
175 var ways = xml.getElementsByTagName("way");
176 for (var i = 0; i < ways.length; i++) {
177 var way = ways[i], nds = way.getElementsByTagName("nd");
180 id: way.getAttribute("id"),
182 nodes: new Array(nds.length),
183 tags: this.getTags(way)
186 for (var j = 0; j < nds.length; j++) {
187 way_object.nodes[j] = nodes[nds[j].getAttribute("ref")];
190 result.push(way_object);
196 getRelations: function (xml, nodes, ways) {
199 var rels = xml.getElementsByTagName("relation");
200 for (var i = 0; i < rels.length; i++) {
201 var rel = rels[i], members = rel.getElementsByTagName("member");
204 id: rel.getAttribute("id"),
206 members: new Array(members.length),
207 tags: this.getTags(rel)
210 for (var j = 0; j < members.length; j++) {
211 if (members[j].getAttribute("type") === "node")
212 rel_object.members[j] = nodes[members[j].getAttribute("ref")];
213 else // relation-way and relation-relation membership not implemented
214 rel_object.members[j] = null;
217 result.push(rel_object);
223 getTags: function (xml) {
226 var tags = xml.getElementsByTagName("tag");
227 for (var j = 0; j < tags.length; j++) {
228 result[tags[j].getAttribute("k")] = tags[j].getAttribute("v");