-
Notifications
You must be signed in to change notification settings - Fork 3
/
taiwanmap.js
122 lines (108 loc) · 3.49 KB
/
taiwanmap.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
var overlay,
mapData,
params = {};
/* Store maps in subdirectory */
var mapsLocation = 'maps/';
var getParameters = function() {
var query,
vars;
/* params */
query = window.location.search.substring(1);
vars = query.split("&");
vars.forEach(function(item) {
var pair = item.split("=");
params[pair[0]] = decodeURI(pair[1]);
});
/* hash */
query = window.location.hash.substring(1);
if (query.length > 0) {
vars = query.split("&");
vars.forEach(function(item) {
var pair = item.split("=");
params[pair[0]] = decodeURI(pair[1]);
});
};
};
$.getJSON( "taiwanmap.json", function( data ) {
console.log(data);
mapData = data;
initMap();
});
var historicalOverlay,
map;
function initMap() {
getParameters();
var locationName = params['location'];
if (!mapData[locationName]) {
locationName = 'Taipei';
console.log("Can't find!");
}
var location = mapData[locationName];
document.title = location['name'] + " / " + locationName;
if (params['lat'] && params['lng'] && params['z']) {
/* load at requested coordinates */
map = new google.maps.Map(document.getElementById('map'), {
zoom: parseInt(params['z']),
center: {lat: parseFloat(params['lat']), lng: parseFloat(params['lng'])},
mapTypeId: google.maps.MapTypeId.SATELLITE
});
} else {
/* load at default coordinates */
map = new google.maps.Map(document.getElementById('map'), {
zoom: location['zoom'],
center: {lat: location['center']['lat'], lng: location['center']['lng']},
mapTypeId: google.maps.MapTypeId.SATELLITE
});
};
var imageBounds = {
north: location['bounds']['north'],
east: location['bounds']['east'],
south: location['bounds']['south'],
west:location['bounds']['west']
};
var mapImage = mapsLocation + location['map'];
$.ajax({
url : mapImage,
cache: true,
processData : false,
success: function() {
historicalOverlay = new google.maps.GroundOverlay(
mapImage,
imageBounds);
historicalOverlay.setMap(map);
historicalOverlay.setOpacity(0.8);
console.log("Finished loading!");
$("#loading-spinner").hide();
}
});
// Set map list dropdown
var maplist = $("#panel > select");
$.each(mapData, function(key, value) {
maplist.append($("<option></option>").attr("value",key).text(value['name'] + " / " + key));
});
maplist.val(locationName);
maplist.change(function() {
var newQuery = $.param({ location: encodeURIComponent(maplist.val()) });
window.location.href = window.location.origin + window.location.pathname + "?" + newQuery;
});
/* Update location bar as map is moved around, so it can be shared */
google.maps.event.addListener(map, 'idle', function() {
document.location.hash = "lat="+this.getCenter().lat().toFixed(6)+
"&lng="+this.getCenter().lng().toFixed(6)+
"&z="+this.getZoom();
});
}
var toggleVisibility = function() {
if (historicalOverlay.getMap()) {
historicalOverlay.setMap(null);
} else {
historicalOverlay.setMap(map);
}
};
var opacitySlider;
var updateOpacity = function() {
historicalOverlay.setOpacity( opacitySlider.val() / 10 );
};
$(document).ready(function() {
opacitySlider = $("#opacity");
});