javascript - How to pass AngularUI Google Map object to a factory or request transformation? -
i have rest service returns geo-coordinates i'm consuming factory using $http in angularjs. in order display these coordinates markers, need create google.maps.marker object take both position , google.maps.map object.
i'm trying figure out best way these data transformations either in factory, or part of transformresponse. unfortunately, angularui maps directive i'm using stores map on local scope.
what best way supply map object either factory or transformresponse?
if you're adamant geocoordinates service returning markers instead of geocoordinates, have 2 options can think of:
a) pass map geocoordinates service
angular.module('mapguy', function($scope, geoservice) { geoservice.addmarkers($scope.map); }). factory('geoservice', function($http) { var addmarkers = function(map) { $http.get('/path/to/geo/server').success(function(points) { angular.foreach(points, function(points) { new google.maps.marker({ map: map, position: new google.maps.latlng(points.lat, points.long) }) }) }); }; return { addmarkers: addmarkers }; }); b) stick map on module's $rootscope , inject $rootscope service
angular.module('mapguy', function($scope, $rootscope, geoservice) { $rootscope.map = $scope.map; geoservice.addmarkers(); }). factory('geoservice', function($http, $rootscope) { var addmarkers = function() { $http.get('/path/to/geo/server').success(function(points) { angular.foreach(points, function(points) { new google.maps.marker({ map: $rootscope.map, position: new google.maps.latlng(points.lat, points.long) }) }) }); }; return { addmarkers: addmarkers }; }); that's rough sketch of code like. i'm pulling google maps api memory, helps idea.
i think first option preferable, it's hard since didn't provide lot of context work with.
Comments
Post a Comment