-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
183 lines (156 loc) · 5.68 KB
/
map.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
// This example adds a search box to a map, using the Google Place Autocomplete
// feature. People can enter geographical searches. The search box will return a
// pick list containing a mix of places and predicted search terms.
// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
var markers = [];
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 37.404001, lng: -122.034976},
zoom: 9,
mapTypeId: 'roadmap'
});
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
var input2 = document.getElementById('pac-input2');
//var searchBox = new google.maps.places.SearchBox(input2);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input2);
var input3 = document.getElementById('pac-input3');
//var searchBox = new google.maps.places.SearchBox(input3);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input3);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
//markers.forEach(function(marker) {
// marker.setMap(null);
// });
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
console.log("markers: " + markers)
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
function middle() {
var totalLat =0;
var totalLng =0;
for (var i=0;i < markers.length; i +=1){
var newlat=markers[i].getPosition().lat();
console.log(markers[i].getPosition().lat())
totalLat = totalLat+ newlat;
console.log("for loop: " + i + " lat: "+totalLat);
totalLng += markers[i].getPosition().lng();
console.log("for loop: " + i + " lng: "+totalLng);
}
var latAvg = totalLat/markers.length;
var lngAvg = totalLng/markers.length;
//console.log("position: " + markers[0]["position"]);
//console.log("position: " + markers[0]["position"][0]);
console.log("lngAvg= " + lngAvg);
console.log("totalLng= " + totalLng );
console.log("latgAvg= " + latAvg);
console.log("totalLat= " + totalLat );
console.log(latAvg + ", " + lngAvg);
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 37.404001, lng: -122.034976},
zoom: 13,
mapTypeId: 'roadmap'
});
var middleMarker = new google.maps.Marker({
position: {lat: latAvg, lng: lngAvg},
map: map,
icon: 'marker.png',
});
}
function clearPage() {
window.location.reload();
}
var map;
function initMap() {
// Create the map.
var pyrmont = {lat: -33.866, lng: 151.196};
map = new google.maps.Map(document.getElementById('map'), {
center: pyrmont,
zoom: 17
});
// Create the places service.
var service = new google.maps.places.PlacesService(map);
var getNextPage = null;
var moreButton = document.getElementById('more');
moreButton.onclick = function() {
moreButton.disabled = true;
if (getNextPage) getNextPage();
};
// Perform a nearby search.
service.nearbySearch(
{location: pyrmont, radius: 500, type: ['store']},
function(results, status, pagination) {
if (status !== 'OK') return;
createMarkers(results);
moreButton.disabled = !pagination.hasNextPage;
getNextPage = pagination.hasNextPage && function() {
pagination.nextPage();
};
});
}
function createMarkers(places) {
var bounds = new google.maps.LatLngBounds();
var placesList = document.getElementById('places');
for (var i = 0, place; place = places[i]; i++) {
var image = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
var marker = new google.maps.Marker({
map: map,
icon: image,
title: place.name,
position: place.geometry.location
});
var li = document.createElement('li');
li.textContent = place.name;
placesList.appendChild(li);
bounds.extend(place.geometry.location);
}
map.fitBounds(bounds);
}