1 OSM.Query = function(map) {
2 var queryButton = $(".control-query .control-button"),
3 uninterestingTags = ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
6 queryButton.on("click", function (e) {
10 if (queryButton.hasClass("active")) {
13 OSM.router.route("/");
20 .on("mouseover", ".query-results li", function () {
21 var geometry = $(this).data("geometry")
22 if (geometry) map.addLayer(geometry);
23 $(this).addClass("selected");
25 .on("mouseout", ".query-results li", function () {
26 var geometry = $(this).data("geometry")
27 if (geometry) map.removeLayer(geometry);
28 $(this).removeClass("selected");
31 function interestingFeature(feature) {
33 for (var key in feature.tags) {
34 if (uninterestingTags.indexOf(key) < 0) {
43 function featurePrefix(feature) {
44 var tags = feature.tags;
47 if (tags.boundary === "administrative") {
48 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level)
50 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
52 for (var key in tags) {
53 var value = tags[key];
56 if (prefixes[key][value]) {
57 return prefixes[key][value];
59 var first = value.substr(0, 1).toUpperCase(),
60 rest = value.substr(1).replace(/_/g, " ");
69 prefix = I18n.t("javascripts.query." + feature.type);
75 function featureName(feature) {
76 var tags = feature.tags;
80 } else if (tags["ref"]) {
82 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
83 return tags["addr:housenumber"] + " " + tags["addr:street"];
85 return "#" + feature.id;
89 function featureLink(feature) {
90 if (feature.type === "area") {
91 if (feature.id >= 3600000000) {
92 var id = feature.id - 3600000000;
94 return "/browse/relation/" + id;
95 } else if (feature.id >= 2400000000) {
96 var id = feature.id - 2400000000;
98 return "/browse/way/" + id;
100 return "/browse/node/" + feature.id;
103 return "/browse/" + feature.type + "/" + feature.id;
107 function featureGeometry(feature, nodes) {
110 if (feature.type === "node") {
111 geometry = L.circleMarker([feature.lat, feature.lon]);
112 } else if (feature.type === "way") {
113 geometry = L.polyline(feature.nodes.map(function (node) {
121 function runQuery(query, $section) {
122 var $ul = $section.find("ul");
127 $section.find(".loader").oneTime(1000, "loading", function () {
132 url: "http://overpass-api.de/api/interpreter",
135 data: "[timeout:5][out:json];" + query,
137 success: function(results) {
140 $section.find(".loader").stopTime("loading").hide();
142 results.elements.forEach(function (element) {
143 if (element.type === "node") {
144 nodes[element.id] = [element.lat, element.lon];
148 for (var i = 0; i < results.elements.length; i++) {
149 var element = results.elements[i];
151 if (interestingFeature(element)) {
153 .data("geometry", featureGeometry(element, nodes))
156 .addClass("inner12 search_results_entry clearfix")
157 .text(featurePrefix(element) + " ")
161 .attr("href", featureLink(element))
162 .text(featureName(element))
170 function queryOverpass(lat, lng) {
171 var latlng = L.latLng(lat, lng),
172 around = "around:10.0," + lat + "," + lng,
173 features = "(node(" + around + ");way(" + around + ");relation(" + around + "))",
174 nearby = "((" + features + ";way(bn));node(w));out;",
175 isin = "(is_in(" + lat + "," + lng + ");>);out;";
177 $("#sidebar_content .query-intro")
181 marker.setLatLng(latlng).addTo(map);
183 marker = L.circle(latlng, 10, { clickable: false }).addTo(map);
186 $(document).everyTime(75, "fadeQueryMarker", function (i) {
188 map.removeLayer(marker);
191 opacity: 0.5 - i * 0.05,
192 fillOpacity: 0.2 - i * 0.02
197 runQuery(nearby, $("#query-nearby"));
198 runQuery(isin, $("#query-isin"));
201 function clickHandler(e) {
202 var precision = OSM.zoomPrecision(map.getZoom()),
203 lat = e.latlng.lat.toFixed(precision),
204 lng = e.latlng.lng.toFixed(precision);
206 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
209 function enableQueryMode() {
210 queryButton.addClass("active");
211 map.on("click", clickHandler);
212 $(map.getContainer()).addClass("query-active");
215 function disableQueryMode() {
216 if (marker) map.removeLayer(marker);
217 $(map.getContainer()).removeClass("query-active");
218 map.off("click", clickHandler);
219 queryButton.removeClass("active");
224 page.pushstate = page.popstate = function(path) {
225 OSM.loadSidebarContent(path, function () {
230 page.load = function(path) {
231 var params = querystring.parse(path.substring(path.indexOf('?') + 1));
233 queryOverpass(params.lat, params.lon);
236 return map.getState();
239 page.unload = function() {