-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
73 lines (70 loc) · 1.89 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
/*global google */
/*exported initMap, displayMap */
// Set the URL to the data file here.
var sourceurl = "data.json";
// Set to true for XHR same-domain loading, set to false for JSONP.
// JSONP files have to be wrapped in 'displayMap(..)'
var useXhr = true;
// This sets where the map should start.
// Default is Linköping, Sweden.
var mapStartPosition = {
lat: 58.4108,
lng: 15.6214
};
function initMap() {
if (useXhr) {
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState === XMLHttpRequest.DONE &&
request.status === 200) {
displayMap(JSON.parse(request.responseText));
}
};
request.open('GET', sourceurl, true);
request.send();
} else {
var script = document.createElement('script');
script.src = sourceurl;
document.getElementsByTagName('head')[0].appendChild(script);
}
}
function displayMap(locations) {
var mapOptions = {
zoom: 14,
center: mapStartPosition,
disableDefaultUI: true,
mapTypeControl: false,
scaleControl: false,
zoomControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: [{
featureType: "poi",
elementType: "labels",
stylers: [{
visibility: "off"
}]
}]
};
var mapWidgetInstance = new google.maps.Map(
document.getElementById('map'),
mapOptions
);
function createMarker(location) {
var infowindow = new google.maps.InfoWindow({
content: "<b>" + location.name + "</b><br>" +
location.address + "<br><br>" +
location.description
});
var marker = new google.maps.Marker({
position: location.pos,
map: mapWidgetInstance,
title: location.name
});
marker.addListener('click', function() {
infowindow.open(mapWidgetInstance, marker);
});
}
for (var i = 0; i < locations.length; i++) {
createMarker(locations[i]);
}
}