Skip to content

Commit 098ddcc

Browse files
committed
Updating with final Step 2 code.
1 parent 6cd1ebe commit 098ddcc

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

step2/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
<script src="js/controllers.js"></script>
1111
</head>
1212
<body>
13+
<div id="persistent-menu">
14+
<button type="button" ng-click="locate()" ng-controller="LocateCtrl">Locate me</button>
15+
</div>
16+
<googlemap id="map_canvas" latitude="37" longitude="-95" zoom="5"></googlemap>
1317
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
1418
</body>
1519
</html>

step2/js/app.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var demoApp = angular.module('demoApp', [
2+
'ngTouch',
3+
'demoControllers'
4+
]);
5+
6+
demoApp.directive('googlemap', function($rootScope) {
7+
return {
8+
restrict: 'E',
9+
replace: true,
10+
template: '<div></div>',
11+
link: function(scope, element, attrs) {
12+
var map = null;
13+
var marker = new google.maps.Marker();
14+
var infowindow = new google.maps.InfoWindow();
15+
var mapOptions = {
16+
zoom: Number(attrs.zoom),
17+
center: new google.maps.LatLng(attrs.latitude, attrs.longitude),
18+
mapTypeId: google.maps.MapTypeId.ROADMAP
19+
};
20+
21+
map = new google.maps.Map(document.getElementById(attrs.id), mapOptions);
22+
23+
scope.$on('locationUpdated', function() {
24+
var latlong = new google.maps.LatLng($rootScope.latitude, $rootScope.longitude);
25+
marker = new google.maps.Marker({ position: latlong, title: "Your Location is " + $rootScope.latitude + ", " + $rootScope.longitude, description: "Hey this is where I'm currently at!", map: map});
26+
map.setZoom(16);
27+
map.panTo(latlong);
28+
29+
google.maps.event.addListener(marker, 'click', function() {
30+
infowindow.setContent("<h2>" + marker.title + "</h2>" + "<p>" + marker.description + "</p>");
31+
infowindow.open(map, marker);
32+
});
33+
});
34+
}
35+
};
36+
});
37+
38+
demoApp.factory('geolocation', ['$q',
39+
function ($q) {
40+
return function () {
41+
var deferred = $q.defer();
42+
var options = { maximumAge: 15000, timeout: 15000, enableHighAccuracy: false };
43+
44+
function onSuccess(position) {
45+
deferred.resolve(position);
46+
}
47+
48+
function onError(error) {
49+
navigator.notification.alert('There was a problem locating your position, please manually enter your city, state or zipcode.', null, 'Failed to Locate Position', 'Close');
50+
deferred.resolve(null);
51+
}
52+
53+
navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
54+
55+
return deferred.promise;
56+
};
57+
}
58+
]);

step2/js/controllers.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var demoControllers = angular.module('demoControllers', []);
2+
3+
demoControllers.controller('LocateCtrl', ['$scope', '$rootScope', 'geolocation',
4+
function($scope, $rootScope, geolocation) {
5+
$scope.locate = function() {
6+
geolocation().then(function(position) {
7+
if(position !== null) {
8+
$rootScope.latitude = position.coords.latitude;
9+
$rootScope.longitude = position.coords.longitude;
10+
$rootScope.$broadcast("locationUpdated");
11+
} else {
12+
alert("Location Lookup Error!");
13+
}
14+
});
15+
};
16+
}
17+
]);

0 commit comments

Comments
 (0)