This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdemo-parking.html
148 lines (125 loc) · 5.38 KB
/
demo-parking.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#mapDiv { display: block; width: 900px; height: 600px; }
</style>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/turf.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=drawing"></script>
</head>
<body onload="initMap()">
<h1 class="title"></h1>
<div id="mapDiv"></div>
<script>
var map, parksData, timer = 2500;
var infoWindow = new google.maps.InfoWindow({ content: "" });
var dataMap = new google.maps.Data();
var bbox = [121.47033691406249, 24.99974967945576, 121.62689208984374, 25.13254167169848];
var data = turf.bboxPolygon(bbox);
var center = turf.center(data);
var styles=[{featureType:"all",elementType:"labels.text.fill",stylers:[{color:"#ffffff"},{weight:"0.20"},{lightness:"28"},{saturation:"23"},{visibility:"off"}]},{featureType:"all",elementType:"labels.text.stroke",stylers:[{color:"#494949"},{lightness:13},{visibility:"off"}]},{featureType:"all",elementType:"labels.icon",stylers:[{visibility:"off"}]},{featureType:"administrative",elementType:"geometry.fill",stylers:[{color:"#000000"}]},{featureType:"administrative",elementType:"geometry.stroke",stylers:[{color:"#144b53"},{lightness:14},{weight:1.4}]},{featureType:"landscape",elementType:"all",stylers:[{color:"#02101b"}]},{featureType:"poi",elementType:"geometry",stylers:[{color:"#0c4152"},{lightness:5}]},{featureType:"road.highway",elementType:"geometry.fill",stylers:[{color:"#000000"}]},{featureType:"road.highway",elementType:"geometry.stroke",stylers:[{color:"#0b434f"},{lightness:25}]},{featureType:"road.arterial",elementType:"geometry.fill",stylers:[{color:"#000000"}]},{featureType:"road.arterial",elementType:"geometry.stroke",stylers:[{color:"#0b3d51"},{lightness:16}]},{featureType:"road.local",elementType:"geometry",stylers:[{color:"#000000"}]},{featureType:"transit",elementType:"all",stylers:[{color:"#146474"}]},{featureType:"water",elementType:"all",stylers:[{color:"#021019"}]}];
function initMap() {
// 地圖初始設定
var mapOptions = {
center: new google.maps.LatLng(25.040667910373205, 121.56516367357992),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP,
// styles: styles
};
var mapElement = document.getElementById("mapDiv");
// Google 地圖初始化
map = new google.maps.Map(mapElement, mapOptions);
setMapStyle();
google.maps.event.addListenerOnce(map, "idle", function() {
$.get('parking.json', function(data){
parksData = data;
var jsonList = createList();
// auto-load per 3 seconds.
for (var i = 0; i < jsonList.length; i++) {
(function(i){
window.setTimeout(function(){
$.get('jsons/' + jsonList[i] + '.json', function(data){
$('.title').text(jsonList[i]);
var geoJson = convertGeoJson(data.result.results);
// var grid = turf.aggregate(hexgrid, geoJson, aggregations);
infoWindow.close();
dataMap.setMap(null);
dataMap = new google.maps.Data();
setMapStyle();
dataMap.addGeoJson(geoJson);
dataMap.setMap(map);
});
}, timer * i);
})(i);
}
}); // $.get('parking.json', function(data){
});
}
function setMapStyle(){
dataMap.addListener('click', function(e) {
if( e.feature.getProperty('used') ){
infoWindow.setContent('<div>' + e.feature.getProperty('name') + ' / 已使用: ' + e.feature.getProperty('used') +'</div>');
}
var anchor = new google.maps.MVCObject();
anchor.set("position", e.latLng);
infoWindow.open(map,anchor);
});
dataMap.setStyle(function(feature) {
if( feature.getProperty('used') ){
//console.log( feature.getProperty('used') );
return {
icon: {
path: google.maps.SymbolPath.CIRCLE,
fillColor: '#A91D1C',
fillOpacity: feature.getProperty('used'),
strokeColor: '#F5C13D',
strokeOpacity: feature.getProperty('used'), //(parseInt( feature.getProperty('availableCar'), 10) > 50) ? 0.75 : 0.3,
scale: (feature.getProperty('used') / 25 > 35) ? 35 : feature.getProperty('used') / 25
}
};
}else{
return { visible: false };
}
});
}
function createList(){
var hr = [], min = ["00", "15", "30", "45"], listArr = [];
for (var i = 10; i <= 19; i++) { hr.push( i+'' ); }
for (var i = 0; i < hr.length; i++) {
for (var j = 0; j < min.length; j++) {
if( hr[i] === "19" && min[j] === '45' ) { break; }
listArr.push('20150813-' + hr[i] + min[j]);
}
}
return listArr;
}
function convertGeoJson(json){
var geoJson, features = [];
// convert to geojson
for (var i = 0; i < json.length; i++) {
// parksData
for (var j = 0; j < parksData.length; j++) {
if( json[i].id === parksData[j].id ){
var totalCar = parseInt(parksData[j].totalCar, 10),
availableCar = parseInt(json[i].availableCar, 10);
features.push(
turf.point([ parksData[j].coords[1], parksData[j].coords[0] ],
{
totalCar: totalCar,
availableCar: availableCar,
used: (totalCar-availableCar < 0) ? 0 : (totalCar-availableCar),
name: parksData[j].name
}));
break;
}
}
}
geoJson = turf.featurecollection(features);
return geoJson;
}
</script>
</body>
</html>