Last active
August 29, 2015 14:07
-
-
Save WaseemTheDream/7a72e4ac59f528133822 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('atlasApp').service('MapsService', function( | |
$scope, | |
$http, | |
LocationProvider, | |
BusInfoProvider) { | |
var MapsService = {}; | |
MapsService.initMap = function() { | |
console.log('Initializing'); | |
var mapOptions = { | |
zoom: 15, | |
center: $scope.mapCenter, | |
mapTypeId: google.maps.MapTypeId.ROADMAP, | |
disableDefaultUI: true, | |
}; | |
MapsService.mapCanvas = document.getElementById('map-canvas'); | |
MapsService.map = new google.maps.Map( | |
MapService.mapCanvas, | |
mapOptions | |
); | |
} | |
MapsService.showMap = function() { | |
MapsService.mapCanvas.hidden = false; | |
} | |
MapsService.hideMap = function() { | |
MapsService.mapCanvas.hidden = true; | |
} | |
MapsService.plotMarker = function(lat, lng, name) { | |
var position = new google.maps.LatLng(lat, lng); | |
$scope.marker = new google.maps.Marker({ | |
position: position, | |
map: $scope.map, | |
title: name | |
}); | |
return position; | |
}; | |
MapsService.setCenter = function(position) { | |
MapsService.map.setCenter(position); | |
}; | |
return { | |
initMap: MapsService.initMap, | |
showMap: MapsService.showMap, | |
hideMap: MapsService.hideMap, | |
plotMarker: MapsService.plotMarker, | |
setCenter: MapsService.setCenter | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The benefit of structuring the Service like this is:
MapsService.instanceVariable
explicitly instead ofthis.instanceVariable
.this
is ambiguous and could be referring to a different object (like in the case of an anonymous inner function).MapsService.mapCanvas
so that the user of this service isn't able to mess with that object and accidentally destroy / change it.