From 9aa6ede63c03f58f57d2905ec4be9d2aa929a5cd Mon Sep 17 00:00:00 2001 From: Norbert Renner Date: Mon, 30 Mar 2015 14:53:35 +0200 Subject: [PATCH] switch search plugin, result-dependent zoom --- README.md | 4 ++-- bower.json | 18 ++++++++-------- js/index.js | 11 ++++++---- js/plugin/Search.js | 50 +++++++++++++++++++++++++++++++-------------- 4 files changed, 53 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index dc7d514..b2d27ae 100644 --- a/README.md +++ b/README.md @@ -88,8 +88,8 @@ Copyright (c) 2013 Felix Bache; [MIT License](https://github.com/MrMufflon/Leafl Copyright (c) 2013, Michael Bostock. All rights reserved.; [3-clause BSD License](https://github.com/mbostock/d3/blob/master/LICENSE) * [Leaflet.draw](https://github.com/Leaflet/Leaflet.draw) Copyright 2012 Jacob Toye; [MIT License](https://github.com/Leaflet/Leaflet.draw/blob/master/MIT-LICENCE.txt) -* [Leaflet.Control.Search](https://github.com/stefanocudini/leaflet-search) -Copyright (c) 2013 Stefano Cudini; [MIT License](https://github.com/stefanocudini/leaflet-search/blob/master/LICENSE.txt) +* [Leaflet Control Geocoder](https://github.com/perliedman/leaflet-control-geocoder) +Copyright (c) 2012 [sa3m](https://github.com/sa3m), Copyright (c) 2013 Per Liedman; [2-clause BSD License](https://github.com/perliedman/leaflet-control-geocoder/blob/master/LICENSE) * [leaflet-plugins](https://github.com/shramov/leaflet-plugins) Copyright (c) 2011-2012, Pavel Shramov; [2-clause BSD License](https://github.com/shramov/leaflet-plugins/blob/master/LICENSE) * [Async.js](https://github.com/caolan/async) diff --git a/bower.json b/bower.json index eb955dc..2c2de74 100644 --- a/bower.json +++ b/bower.json @@ -8,7 +8,6 @@ ], "dependencies": { "leaflet": "~0.7.3", - "leaflet-search": "*", "leaflet-plugins": "~1.3.2", "leaflet-routing": "Turistforeningen/leaflet-routing#gh-pages", "async": "~0.9.2", @@ -16,7 +15,8 @@ "leaflet.draw": "~0.2.3", "bootstrap": "~3.3.4", "DataTables": "~1.10.5", - "Leaflet.Elevation": "MrMufflon/Leaflet.Elevation#master" + "Leaflet.Elevation": "MrMufflon/Leaflet.Elevation#master", + "leaflet-control-geocoder": "~1.1.0" }, "overrides": { "leaflet": { @@ -26,13 +26,6 @@ "dist/images/*.png" ] }, - "leaflet-search": { - "main": [ - "dist/leaflet-search.src.js", - "dist/leaflet-search.min.css", - "images/*.+(png|gif)" - ] - }, "leaflet-plugins": { "main": [ "control/Permalink.js", @@ -66,6 +59,13 @@ }, "Leaflet.Elevation": { "dependencies": null + }, + "leaflet-control-geocoder": { + "main": [ + "Control.Geocoder.js", + "Control.Geocoder.css", + "images/*.+(png|gif)" + ] } } } diff --git a/js/index.js b/js/index.js index d27e733..c82ca1e 100644 --- a/js/index.js +++ b/js/index.js @@ -26,7 +26,7 @@ var topo = L.tileLayer('http://{s}.tile.opentopomap.org/{z}/{x}/{y}.png', { minZoom: 5, - maxZoom: 15, + maxZoom: 16, attribution: 'tiles © OpenTopoMap, CC-BY-SA' + ', SRTM' }); @@ -90,15 +90,14 @@ 'Hiking (Waymarked Trails)': hiking }).addTo(map); - map.addControl(new BR.Search()); - // expose map instance for console debugging BR.debug = BR.debug || {}; BR.debug.map = map; } function initApp() { - var router, + var search, + router, routing, routesLayer, routingOptions, @@ -114,6 +113,9 @@ // left sidebar as additional control position map._controlCorners[leftPaneId] = L.DomUtil.create('div', 'leaflet-' + leftPaneId, map._controlContainer); + search = new BR.Search(); + map.addControl(search); + router = L.bRouter(); //brouterCgi dummyRouter function updateRoute(evt) { @@ -197,6 +199,7 @@ } }); routing.on('routing:routeWaypointEnd routing:setWaypointsEnd', function(evt) { + search.clear(); onUpdate(evt && evt.err); }); diff --git a/js/plugin/Search.js b/js/plugin/Search.js index e40a15b..81ae69a 100644 --- a/js/plugin/Search.js +++ b/js/plugin/Search.js @@ -1,18 +1,38 @@ -BR.Search = L.Control.Search.extend({ +BR.Search = L.Control.Geocoder.extend({ options: { - //url: 'http://nominatim.openstreetmap.org/search?format=json&q={s}', - url: 'http://open.mapquestapi.com/nominatim/v1/search.php?format=json&q={s}', - jsonpParam: 'json_callback', - propertyName: 'display_name', - propertyLoc: ['lat','lon'], - markerLocation: false, - circleLocation: false, - autoType: false, - autoCollapse: true, - minLength: 2, - zoom: 12 + geocoder: new L.Control.Geocoder.Nominatim({ + serviceUrl: 'https://open.mapquestapi.com/nominatim/v1/' + }), + position: 'topleft' }, - - // patch: interferes with draw plugin (adds all layers twice to map?) - _onLayerAddRemove: function() {} + + onAdd: function (map) { + map.attributionControl.addAttribution('Nominatim Search Courtesy of ' + + 'MapQuest' + + ' '); + + return L.Control.Geocoder.prototype.onAdd.call(this, map); + }, + + markGeocode: function(result) { + this._map.fitBounds(result.bbox, { + maxZoom: 17 + }); + + this.clear(); + this._geocodeMarker = new L.CircleMarker(result.center, { + clickable: false, + color: 'red', + opacity: 1, + weight: 3 + }).addTo(this._map); + + return this; + }, + + clear: function() { + if (this._geocodeMarker) { + this._map.removeLayer(this._geocodeMarker); + } + } });