Skip to content

Commit

Permalink
feat(bounds): Extracted the nominatim functionality as a service, to …
Browse files Browse the repository at this point in the history
…be able to use it from center and markers
  • Loading branch information
tombatossals committed Jul 5, 2015
1 parent 4898629 commit 11e9e31
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/directives/bounds.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
angular.module("leaflet-directive").directive('bounds', function ($log, $timeout, $http, leafletMapDefaults, leafletHelpers, leafletBoundsHelpers) {
angular.module("leaflet-directive").directive('bounds', function ($log, $timeout, $http, leafletHelpers, nominatimService, leafletBoundsHelpers) {
return {
restrict: "A",
scope: false,
Expand All @@ -10,16 +10,14 @@ angular.module("leaflet-directive").directive('bounds', function ($log, $timeout
var createLeafletBounds = leafletBoundsHelpers.createLeafletBounds;
var leafletScope = controller[0].getLeafletScope();
var mapController = controller[0];
var errorHeader = leafletHelpers.errorHeader + ' [Controls] ';
var errorHeader = leafletHelpers.errorHeader + ' [Bounds] ';

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();
Expand All @@ -43,22 +41,20 @@ angular.module("leaflet-directive").directive('bounds', function ($log, $timeout
}
});

var lastNominatimQuery;
leafletScope.$watch('bounds', function (bounds) {
if (isDefined(bounds.address)) {
if (isDefined(bounds.address) && bounds.address !== lastNominatimQuery) {
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;
});
nominatimService.query(bounds.address, attrs.id).then(function(data) {
var b = data.boundingbox;
var newBounds = [ [ b[0], b[2]], [ b[1], b[3]] ];
map.fitBounds(newBounds);
}, function(errMsg) {
$log.error(errorHeader + ' ' + errMsg + '.');
});
lastNominatimQuery = bounds.address;
$timeout( function() {
scope.settingBoundsFromScope = false;
});
return;
}
Expand Down
21 changes: 21 additions & 0 deletions src/services/nominatim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
angular.module("leaflet-directive").factory('nominatimService', function ($q, $http, leafletHelpers, leafletMapDefaults) {
var isDefined = leafletHelpers.isDefined;

return {
query: function(address, mapId) {
var defaults = leafletMapDefaults.getDefaults(mapId);
var url = defaults.nominatim.server;
var df = $q.defer();

$http.get(url, { params: { format: 'json', limit: 1, q: address } }).success(function(data) {
if (data.length > 0 && isDefined(data[0].boundingbox)) {
df.resolve(data[0]);
} else {
df.reject('[Nominatim] Invalid address');
}
});

return df.promise;
}
};
});

0 comments on commit 11e9e31

Please sign in to comment.