forked from runyanjake/JRAW-CruzHacks-2018
/
PinUser.js
305 lines (258 loc) · 8.74 KB
/
PinUser.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//this function sets the min on the map and sets the camera to it
/*function pinMe(latit, long) {
var coords = {lat: latit, lng: long};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: coords
});
var marker = new google.maps.Marker({
position: coords,
map: map
});
}
//this function gets the current position
function onPositionUpdate(position){
var lat = position.coords.latitude;
var lng = position.coords.longitude;
pinMe(lat, lng);
}
//this function is the main function. It calls all the functions either directly and/or indirectly
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(onPositionUpdate);
}else{
alert("geolocation is not currently available");
}
*/
//----------------------------------------new working multiple pins-----------------------------------------
//coordinate object
var coordinates = function(lats,longs, titles){
this.lats = lats;
this.longs = longs;
this.titles = titles;
}
//initializes empty array
function initArray(){
var coordArray = [];
return coordArray;
}
//pushes a new coordinate onto the list specified
function pushCoord(coordArr,lats,longs, titles){
var coord = new coordinates(lats,longs, titles);
coordArr.push(coord);
}
//initialize map
var map, infoWindow;
var currentPosition;
var text = '{ "toilet_lat":"33.44", "toilet_lon":"-34", "name":"Uluru"}';
var toilet = JSON.parse(text);
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.loc_id = 123456;
$scope.name = toilet.name;
$scope.rating = "Clean";
$scope.notes = "I had an amazing experience";
});
function initMap() {
//initialize map
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 20
});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
currentPosition = {
lat: position.coords.latitude,
lng: position.coords.longitude
}
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow = new google.maps.InfoWindow;
infoWindow.setPosition(pos);
infoWindow.setContent('You Are Here.');
infoWindow.open(map);
var coordList = initArray();
var testCoord = new coordinates(10.1,25.0);
coordList.push(testCoord);
pushCoord(coordList,20.1,2.0);
coordList.pop();
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
//------newly added-------//
var coordList = initArray();
// var testCoord = new coordinates(10.1, 25.0);
// coordList.pushCoord(testCoord);
pushCoord(coordList, 10.1, 2.0, "Random Marker");
pushCoord(coordList, 20.1, 2.0, "Random Marker");
//-----------------------//
//------newly added-------//
var markerList = initArray();
for (var index = 0; index < coordList.length; index++) {
var pos = new google.maps.LatLng((coordList[index].lats),(coordList[index].longs));
markerList[index] = new google.maps.Marker({
position: pos,
title: coordList[index].titles
});
markerList[index].setMap(map);
}
// for (var index = 0; index < markerList.length; index++) {
// markerList[index].setMap(map);
// }
//-------------------------//
infowindow = new google.maps.InfoWindow({
content: document.getElementById('form')
});
messagewindow = new google.maps.InfoWindow({
content: document.getElementById('message')
});
google.maps.event.addListener(map, 'click', function(event) {
marker = new google.maps.Marker({
position: event.latLng,
map: map
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
});
}
function saveData() {
var name = escape(document.getElementById('name').value);
var rating = escape(document.getElementById('rating').value);
var latlng = marker.getPosition();
var url = 'phpsqlinfo_addrow.php?name=' + name + '&rating=' + rating + '&lat=' + latlng.lat() + '&lng=' + latlng.lng();
downloadUrl(url, function(data, responseCode) {
if (responseCode == 200 && data.length <= 1) {
infowindow.close();
messagewindow.open(map, marker);
}
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request.responseText, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing () {}
// Drops Random Marker
function dropRandMarker(){
var randPos = new google.maps.LatLng(Math.random() * 170.0 - 85,Math.random() * 360.0 - 180);
dropNewMarker(randPos);
}
// Drops a New Marker
function dropNewMarker(position){
map.setCenter(position);
var marker = new google.maps.Marker({
position: position,
title: "Random Marker"
});
marker.setMap(map);
}
// Drops a New Marker
function dropMarkerAtLocation(){
dropNewMarker(currentPosition);
}
// Handles Location Error
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
//initialize map
function oldInitMap() {
system.log("initMap");
//get my lat/long
var myLatlng = new google.maps.LatLng(100,200);
//create new map
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
//create new location marker
var newLocationMarker = new google.maps.Marker({
position: myLatlng,
title:"Current Location"
});
//add to new map
newLocationMarker.setMap(map);
//updates position and plugs it into a new marker that is set onto the map
function onPositionUpdate(position){
system.log("onPositionUpdate");
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var myLatlng = new google.maps.LatLng(lat,lng);
var mapOptions = {
zoom: 4,
center: myLatlng
}
var marker3 = new google.maps.Marker({
position: myLatlng,
title:"current location"
});
marker3.setMap(map);
}
//pulls current lat and long and plugs it into onPositionUpdate
function pinCurrLoc(){
system.log("pinCurrLoc");
if(navigator.geolocation)
navigator.geolocation.getCurrentPosition(onPositionUpdate);
else
alert("navigator.geolocation is not available");
}
pinCurrLoc();
}
function newPinDrop(position){
system.log("newPinDrop");
//infoWindow = new google.maps.InfoWindow;
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
var position = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
},function(){
console.log("Couldn't get location.");
});
}else{
console.out("No geolocation supported, must consent.");
}
console.log("Test 2: Lat: " + position.lat + " Lng: " + position.lng);
//var myLatlng = new google.maps.LatLng(position.lat,position.lng);
var myLatlng = new google.maps.LatLng(100,200); //<<<<<<this is what causes permission request
//bad?
var mapOptions = {
zoom: 4,
center: myLatlng
}
console.log("Test 3: Lat: " + position.lat + " Lng: " + position.lng);
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
///bad?
var newMarker = new google.maps.Marker({
position: myLatlng,
title:"Current Location"
});
newMarker.setMap(map);
}
//var text = '{ "toilet_lat":"33.44", "toilet_lon":"-34", "name":"Uluru"}';
//var obj = JSON.parse(text);
//var app = angular.module('myApp', []);
//app.controller('myCtrl', function ($scope) {
// $scope.loc_id = 123456;
// $scope.name = toilet.name;
// $scope.rating = "Clean";
// $scope.notes = "I had an amazing experience";
//});