Skip to content

Commit

Permalink
feat(bounds): Added the nominatim address way of setting bounds, as r…
Browse files Browse the repository at this point in the history
…equested by @stefan-niedermann here:

#622
  • Loading branch information
tombatossals committed Jul 4, 2015
1 parent ae3396b commit 6e16cad
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
41 changes: 28 additions & 13 deletions src/directives/bounds.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
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 &&
bounds._northEast.lat === 0 && bounds._northEast.lng === 0);
};

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;
}
Expand All @@ -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;
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/directives/center.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
5 changes: 4 additions & 1 deletion src/services/leafletMapDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 6e16cad

Please sign in to comment.