From 6e16cad065e68345e6f965838842717205038e4a Mon Sep 17 00:00:00 2001 From: David Rubert Date: Sat, 4 Jul 2015 18:57:42 +0200 Subject: [PATCH] feat(bounds): Added the nominatim address way of setting bounds, as requested by @stefan-niedermann here: https://github.com/tombatossals/angular-leaflet-directive/issues/622 --- src/directives/bounds.js | 41 ++++++++++++++++++++---------- src/directives/center.js | 2 +- src/services/leafletMapDefaults.js | 5 +++- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/directives/bounds.js b/src/directives/bounds.js index 0c2de433..6a0539ce 100644 --- a/src/directives/bounds.js +++ b/src/directives/bounds.js @@ -1,15 +1,16 @@ -angular.module("leaflet-directive").directive('bounds', function ($log, $timeout, leafletHelpers, leafletBoundsHelpers) { +angular.module("leaflet-directive").directive('bounds', function ($log, $timeout, $http, leafletMapDefaults, leafletHelpers, leafletBoundsHelpers) { return { restrict: "A", scope: false, replace: false, - require: [ 'leaflet', 'center' ], + require: [ 'leaflet' ], link: function(scope, element, attrs, controller) { - var isDefined = leafletHelpers.isDefined, - createLeafletBounds = leafletBoundsHelpers.createLeafletBounds, - leafletScope = controller[0].getLeafletScope(), - mapController = controller[0]; + var isDefined = leafletHelpers.isDefined; + var createLeafletBounds = leafletBoundsHelpers.createLeafletBounds; + var leafletScope = controller[0].getLeafletScope(); + var mapController = controller[0]; + var errorHeader = leafletHelpers.errorHeader + ' [Controls] '; var emptyBounds = function(bounds) { return (bounds._southWest.lat === 0 && bounds._southWest.lng === 0 && @@ -17,10 +18,12 @@ angular.module("leaflet-directive").directive('bounds', function ($log, $timeout }; mapController.getMap().then(function (map) { + var defaults = leafletMapDefaults.getDefaults(attrs.id); + leafletScope.$on('boundsChanged', function (event) { var scope = event.currentScope; var bounds = map.getBounds(); - //$log.debug('updated map bounds...', bounds); + if (emptyBounds(bounds) || scope.settingBoundsFromScope) { return; } @@ -36,23 +39,35 @@ angular.module("leaflet-directive").directive('bounds', function ($log, $timeout options: bounds.options }; if (!angular.equals(scope.bounds, newScopeBounds)) { - //$log.debug('Need to update scope bounds.'); scope.bounds = newScopeBounds; } }); + leafletScope.$watch('bounds', function (bounds) { - //$log.debug('updated bounds...', bounds); - if (!isDefined(bounds)) { - $log.error('[AngularJS - Leaflet] Invalid bounds'); + if (isDefined(bounds.address)) { + scope.settingBoundsFromScope = true; + var url = defaults.nominatim.server; + $http.get(url, { params: { format: 'json', limit: 1, q: bounds.address } }).success(function(data) { + if (data.length > 0 && isDefined(data[0].boundingbox)) { + var b = data[0].boundingbox; + var newBounds = [ [ b[0], b[2]], [ b[1], b[3]] ]; + map.fitBounds(newBounds); + } else { + $log.error(errorHeader + ' Invalid Nominatim address.'); + } + + $timeout( function() { + scope.settingBoundsFromScope = false; + }); + }); return; } + var leafletBounds = createLeafletBounds(bounds); if (leafletBounds && !map.getBounds().equals(leafletBounds)) { - //$log.debug('Need to update map bounds.'); scope.settingBoundsFromScope = true; map.fitBounds(leafletBounds, bounds.options); $timeout( function() { - //$log.debug('Allow bound updates.'); scope.settingBoundsFromScope = false; }); } diff --git a/src/directives/center.js b/src/directives/center.js index 73e17bc6..02cb237c 100644 --- a/src/directives/center.js +++ b/src/directives/center.js @@ -34,7 +34,7 @@ angular.module("leaflet-directive").directive('center', var defaults = leafletMapDefaults.getDefaults(attrs.id); if (attrs.center.search("-") !== -1) { - $log.error(errorHeader + ' The "center" variable can\'t use a "-" on his key name: "' + attrs.center + '".'); + $log.error(errorHeader + ' The "center" variable can\'t use a "-" on its key name: "' + attrs.center + '".'); map.setView([defaults.center.lat, defaults.center.lng], defaults.center.zoom); return; } else if (shouldInitializeMapWithBounds(leafletScope.bounds, centerModel)) { diff --git a/src/services/leafletMapDefaults.js b/src/services/leafletMapDefaults.js index acf39206..1844ff3c 100644 --- a/src/services/leafletMapDefaults.js +++ b/src/services/leafletMapDefaults.js @@ -19,6 +19,9 @@ angular.module("leaflet-directive").factory('leafletMapDefaults', function ($q, collapsed: true } }, + nominatim: { + server: ' http://nominatim.openstreetmap.org/search' + }, crs: L.CRS.EPSG3857, tileLayer: '//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', tileLayerOptions: { @@ -156,7 +159,7 @@ angular.module("leaflet-directive").factory('leafletMapDefaults', function ($q, if (isDefined(userDefaults.map)) { newDefaults.map = userDefaults.map; } - + if (isDefined(userDefaults.path)) { newDefaults.path = userDefaults.path; }