AngularJS, Leaflet. How can I get lat and lon coordinates dynamically from a third-party service? AngularJS, Leaflet. How can I get lat and lon coordinates dynamically from a third-party service? json json

AngularJS, Leaflet. How can I get lat and lon coordinates dynamically from a third-party service?


Not quite sure what you mean by 'How can I make sure that the data (Lat and Lon) came dynamically', but you could utilize the HTML5 geolocation function: navigator.geolocation.getCurrentPosition(success[, error[, options]])

You should abstract this into a seperate service or factory to make it more reusable and to avoid dependecy to window

angular.module('app', []).factory('GeolocationService', ['$q', '$window', function ($q, $window) {    function getPosition() {      var deferred = $q.defer();      if (!$window.navigator.geolocation) { // check if geolocation is supported        deferred.reject('Geolocation is not supported.');        return;    }    $window.navigator.geolocation.getCurrentPosition( // get the current position         function (position) { // ok            deferred.resolve(position);        },        function (err) { // error            deferred.reject(err);        });    return deferred.promise; // returned as a promise  }  return {    getCurrentPosition: getCurrentPosition  };}]);

You could then call it from you controller something like this

geolocationSvc.getPosition().then(   function success(position){     $scope.mapCenter = {      lat: pos.coords.latitude,      lng: pos.coords.longitude,      zoom: 17      }, function error(err){  console.log('ERROR(' + err.code + '): ' + err.message);});

Note that this code has not been actually tested, but should provide you with an outline of how this could be implemented.