-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (76 loc) · 2.33 KB
/
index.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
let map;
function initMap(position) {
const rio = new google.maps.LatLng(-22.958296, -43.257151);
if (position.coords) {
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
zoom: 12
});
}
else {
map = new google.maps.Map(document.getElementById("map"), {
center: rio,
zoom: 4
});
}
fetch("./places.json")
.then(function(response) {
return response.json();
})
.then(function(places){
var markers = [];
var info = []
var markersInfo = new Object();
markersInfo.markers = markers;
markersInfo.info = info;
for (var i in places) {
var place = places[i];
var result = createMarker(
place.name, place.geo, place.url, place.food, place.food_desc, place.work_hours, place.contact);
markersInfo.info.push(result.info);
markersInfo.markers.push(result.marker);
};
return markersInfo;
}).then(function(markersInfo){
const markers = markersInfo.markers;
const algorithm = new markerClusterer.GridAlgorithm({});
new markerClusterer.MarkerClusterer({ algorithm, markers, map });
return markersInfo.info;
})
.then(function(info){
google.maps.event.addListener(map, "click", function(event) {
for (var i = 0; i < info.length; i++ ) {
info[i].close();
};
});
})
}
function checkAndInit(){
if (navigator.geolocation) {
return navigator.geolocation.getCurrentPosition(initMap, initMap);
} else {
return initMap({});
}
}
function createMarker(name, geo, url, food, foodDesc, workHours, contact) {
var marker = new google.maps.Marker({
map,
position: geo,
title: name
});
var message = "<h3><a href='"+url+"' target='_blank'>"+name+"</a></h3><p><strong>"+food+"</strong><br />"+foodDesc+"</p>";
if (workHours !== ''){
message += "<p><br />Aberto: "+workHours+"</p>"
}
if (contact !== ''){
message += "<p><br />Contato: "+contact+"</p>"
}
var infowindow = new google.maps.InfoWindow({
content: message
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
return {info: infowindow, marker: marker};
}
window.initMap = checkAndInit;