From 39e4608c4e1ce8f221af2c70da3d6396cc370262 Mon Sep 17 00:00:00 2001 From: Jon Atkins Date: Wed, 11 Feb 2015 23:04:08 +0000 Subject: [PATCH] search: draw a marker highlighting search results (rectangle for region, 10px circle for point) --- code/geosearch.js | 75 ++++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 27 deletions(-) diff --git a/code/geosearch.js b/code/geosearch.js index 14d9db50..27c8b124 100644 --- a/code/geosearch.js +++ b/code/geosearch.js @@ -4,7 +4,7 @@ window.setupGeosearch = function() { $('#geosearch').keypress(function(e) { if((e.keyCode ? e.keyCode : e.which) != 13) return; - + var search = $(this).val(); if ( window.search(search) ) return; @@ -15,36 +15,57 @@ window.setupGeosearch = function() { map.locate({setView : true, maxZoom: 13}); }); $('#geosearch').keyup(function(){$(this).removeClass('search_not_found')}); + + window.searchResultLayer = L.layerGroup(); + map.addLayer(window.searchResultLayer); } window.search = function(search) { - if (!runHooks('geoSearch', search)) { - return true; - } - - if(search.split(",").length == 2) { - var ll = search.split(","); - if(!isNaN(ll[0]) && !isNaN(ll[1])) { - ll = [parseFloat(ll[0]), parseFloat(ll[1])]; - if(ll[0] >= -90 && ll[0] <= 90 && ll[1] >= -180 && ll[1] <= 180) { - window.map.setView(L.latLng(ll[0], ll[1]), 17); - if(window.isSmartphone()) window.show('map'); - return true; - } - } - } + window.searchResultLayer.clearLayers(); - $.getJSON(NOMINATIM + encodeURIComponent(search), function(data) { - if(!data || !data[0]) { - $('#geosearch').addClass('search_not_found'); + var searchMarkerOptions = {color:'red', weight:3, opacity: 0.5, fill:false, dashArray:"5,5", clickable:false}; + + + if (!runHooks('geoSearch', search)) { + return true; + } + + if(search.split(",").length == 2) { + var ll = search.split(","); + if(!isNaN(ll[0]) && !isNaN(ll[1])) { + ll = [parseFloat(ll[0]), parseFloat(ll[1])]; + if(ll[0] >= -90 && ll[0] <= 90 && ll[1] >= -180 && ll[1] <= 180) { + var ll = L.latLng(ll); + window.map.setView(ll, 17); + + window.searchResultLayer.clearLayers(); + window.searchResultLayer.addLayer(L.circleMarker(ll,searchMarkerOptions)); + + if(window.isSmartphone()) window.show('map'); return true; } - var b = data[0].boundingbox; - if(!b) return true; - var southWest = new L.LatLng(b[0], b[2]), - northEast = new L.LatLng(b[1], b[3]), - bounds = new L.LatLngBounds(southWest, northEast); - window.map.fitBounds(bounds, {maxZoom: 17}); - if(window.isSmartphone()) window.show('map'); - }); + } + } + + $.getJSON(NOMINATIM + encodeURIComponent(search), function(data) { + if(!data || !data[0]) { + $('#geosearch').addClass('search_not_found'); + return true; + } + var b = data[0].boundingbox; + if(!b) return true; + var southWest = new L.LatLng(b[0], b[2]), + northEast = new L.LatLng(b[1], b[3]), + bounds = new L.LatLngBounds(southWest, northEast); + + window.searchResultLayer.clearLayers(); + if (southWest.equals(northEast)) { + window.searchResultLayer.addLayer(L.circleMarker(southWest,searchMarkerOptions)); + } else { + window.searchResultLayer.addLayer(L.rectangle(bounds,searchMarkerOptions)); + } + + window.map.fitBounds(bounds, {maxZoom: 17}); + if(window.isSmartphone()) window.show('map'); + }); }