-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathable-map.js
More file actions
181 lines (171 loc) · 4.55 KB
/
able-map.js
File metadata and controls
181 lines (171 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
(function() {
// 'use strict';
angular.module('able').directive('mapElement', mapDirective)
function mapDirective() {
var directive = {
restrict: 'A',
controller: mapController,
controllerAs: 'map',
bindToController: true
}
return directive
}
function mapController($q, $state, $rootScope, $scope) {
var vm = this
$scope.selectedItem
$scope.searchText
$scope.search = search
$scope.place = place
vm.setCartAddress = setCartAddress
vm.loading = true
var placer = new google.maps.places.PlacesService(document.getElementById('mapping2'))
var autocomplete = $rootScope.autocomplete
var geocoder = $rootScope.geocoder
var map
if ($rootScope.located) {
setMap()
getAddress($rootScope.latitude, $rootScope.longitude)
} else {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
$rootScope.latitude = position.coords.latitude
$rootScope.longitude = position.coords.longitude
$rootScope.accuracy = position.coords.accuracy
getAddress($rootScope.latitude, $rootScope.longitude)
$rootScope.located = true
$rootScope.locating = false
setMap()
}, function() {
vm.loading = false
});
} else {
vm.loading = false
}
}
function setMap() {
map = new google.maps.Map(document.getElementById('mapping'), {
center: {
'lat': $rootScope.latitude,
'lng': $rootScope.longitude
},
zoom: 18,
disableDefaultUI: true,
zoomControl: true
})
setListeners()
}
function setListeners() {
map.addListener('dragend', function() {
getAddress(map.getCenter().lat(), map.getCenter().lng())
$rootScope.latitude = map.getCenter().lat()
$rootScope.longitude = map.getCenter().lng()
});
map.addListener('drag', function() {
$rootScope.latitude = map.getCenter().lat()
$rootScope.longitude = map.getCenter().lng()
});
map.addListener('idle', function() {
map.setCenter({
'lat': $rootScope.latitude,
'lng': $rootScope.longitude
});
google.maps.event.trigger(map, "resize");
});
map.addListener('resize', function() {
map.setCenter({
'lat': $rootScope.latitude,
'lng': $rootScope.longitude
});
});
$scope.$watch(function w(scope) {
return ($rootScope.height)
}, function c(n, o) {
google.maps.event.trigger(map, "resize");
});
vm.loading = false
}
function search(address) {
vm.searching = true
if (address) {
var deferred = $q.defer();
getAutocomplete(address).then(function(predictions) {
if (predictions) {
var results = [];
for (var i = 0, prediction; prediction = predictions[i]; i++) {
results.push(prediction);
}
deferred.resolve(results);
}
});
return deferred.promise;
}
return
}
function getAutocomplete(address) {
var deferred = $q.defer();
var loc = new google.maps.LatLng($rootScope.latitude, $rootScope.longitude)
var request = {
input: address,
location: loc,
radius: 500
}
autocomplete.getQueryPredictions(request, function(data) {
deferred.resolve(data);
});
return deferred.promise;
}
function place(item) {
if (item) {
vm.searching = false
var place_id = item.place_id
$rootScope.place = item
$rootScope.addressed = true
$rootScope.located = true
$rootScope.locating = false
var deferred = $q.defer();
getPlace(place_id).then(function(infos) {
$rootScope.place.address_components = infos.address_components
$rootScope.latitude = infos.geometry.location.lat()
$rootScope.longitude = infos.geometry.location.lng()
$rootScope.accuracy = 0
map.panTo(infos.geometry.location)
$rootScope.addressed = true
});
return deferred.promise;
}
}
function getPlace(place_id) {
var deferred = $q.defer();
placer.getDetails({
placeId: place_id
}, function(data) {
deferred.resolve(data);
});
return deferred.promise;
}
function getAddress(lat, lng) {
geocoder.geocode({
'location': {
'lat': lat,
'lng': lng
}
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
$scope.selectedItem = {
description: results[0].formatted_address,
place_id: results[0].place_id
}
$rootScope.place = $scope.selectedItem
$scope.searchText = results[0].formatted_address
$scope.$apply()
} else {}
});
}
function setCartAddress() {
$rootScope.place_verified = true
$rootScope.updateDistance()
// $rootScope.updateCart()
$state.go('storePage.offerPage')
}
}
})();