-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
185 lines (153 loc) · 5.2 KB
/
main.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
// TODO
// Use the data from the openbrewerydb API to display the breweries on the google maps.
// Find a solution for grouping nearby breweries markers together
// If the user clicks on the group then there we generate a list of all items from the selected group (use angular)
// If we select an item from the list then we zoom into brewerie marker.
// And for last if we click on the marker then we show a popup with the name of the bar.
let api = `https://api.openbrewerydb.org/breweries`;
function fetchData(callback) {
return fetch(api).then((resp) => {
return resp.json();
})
.then(callback)
.catch(err => {
console.error('Problems when fetching data: ', err);
})
}
// Map parameters
let map;
let markers;
let googleInsance;
const startZoom = 8;
const startCordinates = {
lat: 33.524521,
lng: -86.774322
};
let centerControl;
let centerControlDiv = document.createElement('div');
/**
* The CenterControl adds a control to the map that recenters the map on
* Chicago.
* @constructor
* @param {!Element} controlDiv
* @param {!google.maps.Map} map
* @param {?google.maps.LatLng} center
*/
function CenterControl(controlDiv, map, markers) {
// We set up a variable for this since we're adding event listeners
// later.
var control = this;
// Set the center property upon construction
control.marker_ = markers;
control.selected_ = null;
controlDiv.style.clear = 'both';
var goCenterUI = document.createElement('div');
goCenterUI.id = 'goCenterUI';
goCenterUI.title = 'Click to recenter the map';
controlDiv.appendChild(goCenterUI);
markers.forEach(element => {
// Set CSS for the control border
var goCenterText = document.createElement('div');
goCenterText.id = 'goCenterText';
goCenterText.innerHTML = element.name;
goCenterText.title = element.id
goCenterUI.appendChild(goCenterText);
// Set up the click event listener for 'Center Map': Set the center of
// the map
// to the current center of the control.
goCenterUI.addEventListener('click', function(click) {
control.getMarker().forEach(element => {
if(element.name === click.target.innerText) {
map.setZoom(17);
map.panTo(new google.maps.LatLng( element.latitude, element.longitude) );
}
});
});
});
// Set up the click event listener for 'Set Center': Set the center of
// the control to the current center of the map.
// setCenterUI.addEventListener('click', function() {
// var newCenter = map.getCenter();
// control.setCenter(newCenter);
// });
}
/**
* Define a property to hold the center state.
* @private
*/
CenterControl.prototype.marker_ = null;
/**
* Gets the map center.
* @return {?google.maps.LatLng}
*/
CenterControl.prototype.getMarker = function() {
return this.marker_;
};
/**
* Sets the map center.
* @param {?google.maps.LatLng} Marker
*/
CenterControl.prototype.setMarker = function(markers, map) {
this.marker_ = markers;
};
function initMap() {
googleInsance = google;
map = new google.maps.Map(document.getElementById('map'), {
center: startCordinates,
zoom: startZoom
});
markers = [];
};
angular.module('mapApp', [])
.controller('MarkersListController', function($scope) {
function addtomap(element, icon) {
var infowindow = new google.maps.InfoWindow({
content: element.name
});
var image = 'http://pngimg.com/uploads/beer/beer_PNG2333.png';
var marker = new google.maps.Marker({
position: new google.maps.LatLng(element.latitude, element.longitude),
map: map,
name: element.name,
element_id: element.id,
latitude: element.latitude,
longitude: element.longitude,
icon: {
size: new google.maps.Size(40, 40),
scaledSize: new google.maps.Size(40, 40),
url: image
},
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
markers.push(marker);
}
// fetching of user data;
fetchData((data) => {
console.log('Loaction data', data);
data.forEach(element => {
addtomap(element);
});
// Add a marker clusterer to manage the markers.
markerCluster = new MarkerClusterer(map, markers,
{
zoomOnClick: false,
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
googleInsance.maps.event.addListener(markerCluster, 'clusterclick', function(cluster) {
// Create the DIV to hold the control and call the CenterControl()
// constructor
// passing in this DIV.
if (centerControl) {
// map.controls[google.maps.ControlPosition.RIGHT_TOP].removeAt(0);
map.controls[google.maps.ControlPosition.RIGHT_TOP].clear();
centerControlDiv = document.createElement('div');
}
centerControl = new CenterControl(centerControlDiv, map, cluster.markers_);
centerControlDiv.index = 1;
centerControlDiv.style['padding-top'] = '10px';
map.controls[google.maps.ControlPosition.RIGHT_TOP].push(centerControlDiv);
});
});
});