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'],
7 queryButton.on("click", function (e) {
11 if (queryButton.hasClass("active")) {
14 OSM.router.route("/");
21 .on("mouseover", ".query-results li", function () {
22 var geometry = $(this).data("geometry")
23 if (geometry) map.addLayer(geometry);
24 $(this).addClass("selected");
26 .on("mouseout", ".query-results li", function () {
27 var geometry = $(this).data("geometry")
28 if (geometry) map.removeLayer(geometry);
29 $(this).removeClass("selected");
32 function interestingFeature(feature, origin) {
34 if (feature.type === "node" &&
35 OSM.distance(origin, L.latLng(feature.lat, feature.lon)) > searchRadius) {
39 for (var key in feature.tags) {
40 if (uninterestingTags.indexOf(key) < 0) {
49 function featurePrefix(feature) {
50 var tags = feature.tags;
53 if (tags.boundary === "administrative") {
54 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level)
56 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
58 for (var key in tags) {
59 var value = tags[key];
62 if (prefixes[key][value]) {
63 return prefixes[key][value];
65 var first = value.substr(0, 1).toUpperCase(),
66 rest = value.substr(1).replace(/_/g, " ");
75 prefix = I18n.t("javascripts.query." + feature.type);
81 function featureName(feature) {
82 var tags = feature.tags;
86 } else if (tags["ref"]) {
88 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
89 return tags["addr:housenumber"] + " " + tags["addr:street"];
91 return "#" + feature.id;
95 function featureLink(feature) {
96 if (feature.type === "area") {
97 if (feature.id >= 3600000000) {
98 var id = feature.id - 3600000000;
100 return "/browse/relation/" + id;
101 } else if (feature.id >= 2400000000) {
102 var id = feature.id - 2400000000;
104 return "/browse/way/" + id;
106 return "/browse/node/" + feature.id;
109 return "/browse/" + feature.type + "/" + feature.id;
113 function featureGeometry(feature, nodes) {
116 if (feature.type === "node") {
117 geometry = L.circleMarker([feature.lat, feature.lon]);
118 } else if (feature.type === "way") {
119 geometry = L.polyline(feature.nodes.map(function (node) {
127 function runQuery(latlng, query, $section) {
128 var $ul = $section.find("ul");
133 $section.find(".loader").oneTime(1000, "loading", function () {
138 url: "http://overpass-api.de/api/interpreter",
141 data: "[timeout:5][out:json];" + query,
143 success: function(results) {
146 $section.find(".loader").stopTime("loading").hide();
148 results.elements.forEach(function (element) {
149 if (element.type === "node") {
150 nodes[element.id] = [element.lat, element.lon];
154 for (var i = 0; i < results.elements.length; i++) {
155 var element = results.elements[i];
157 if (interestingFeature(element, latlng)) {
159 .data("geometry", featureGeometry(element, nodes))
162 .addClass("inner12 search_results_entry clearfix")
163 .text(featurePrefix(element) + " ")
167 .attr("href", featureLink(element))
168 .text(featureName(element))
176 function queryOverpass(lat, lng) {
177 var latlng = L.latLng(lat, lng),
178 around = "around:" + searchRadius + "," + lat + "," + lng,
179 features = "(node(" + around + ");way(" + around + ");relation(" + around + "))",
180 nearby = "((" + features + ";way(bn));node(w));out;",
181 isin = "(is_in(" + lat + "," + lng + ");>);out;";
183 $("#sidebar_content .query-intro")
187 marker.setLatLng(latlng).addTo(map);
189 marker = L.circle(latlng, searchRadius, { clickable: false }).addTo(map);
192 $(document).everyTime(75, "fadeQueryMarker", function (i) {
194 map.removeLayer(marker);
197 opacity: 0.5 - i * 0.05,
198 fillOpacity: 0.2 - i * 0.02
203 runQuery(latlng, nearby, $("#query-nearby"));
204 runQuery(latlng, isin, $("#query-isin"));
207 function clickHandler(e) {
208 var precision = OSM.zoomPrecision(map.getZoom()),
209 lat = e.latlng.lat.toFixed(precision),
210 lng = e.latlng.lng.toFixed(precision);
212 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
215 function enableQueryMode() {
216 queryButton.addClass("active");
217 map.on("click", clickHandler);
218 $(map.getContainer()).addClass("query-active");
221 function disableQueryMode() {
222 if (marker) map.removeLayer(marker);
223 $(map.getContainer()).removeClass("query-active");
224 map.off("click", clickHandler);
225 queryButton.removeClass("active");
230 page.pushstate = page.popstate = function(path) {
231 OSM.loadSidebarContent(path, function () {
236 page.load = function(path) {
237 var params = querystring.parse(path.substring(path.indexOf('?') + 1));
239 queryOverpass(params.lat, params.lon);
242 return map.getState();
245 page.unload = function() {