-
Notifications
You must be signed in to change notification settings - Fork 0
/
subway.js
141 lines (121 loc) · 4.38 KB
/
subway.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
const changeMapMode = (mode) => {
location.hash = `#${mode}`;
location.reload();
}
const changeCity = (city) => {
changeUrlParam('city', city);
}
const changeUrlParam = (key, value) => {
const currentUrlSearch = window.location.search;
const searchParams = new URLSearchParams(currentUrlSearch);
searchParams.set(key, value);
window.location.search = `?${searchParams.toString()}`;
}
const getUrlParam = (key) => {
const currentUrlSearch = window.location.search;
const searchParams = new URLSearchParams(currentUrlSearch);
const value = searchParams.get(key);
return value;
}
const getCityAdcode = async cityName => {
const citiesAdcode = await (await fetch("data/cityToAdcode.json")).json();
return citiesAdcode[cityName];
};
const getSubwayInfo = async (cityName, cityAdcode) => {
return await (await fetch(`https://webapi.amap.com/subway/data/${cityAdcode}_drw_${cityName}.json?uid=1521018060609`)).json();
};
const getStationsCoordinates = subwayInfo => {
let stationsCoordinates = {};
subwayInfo.l.forEach(line => {
line.st.forEach(station => {
stationsCoordinates[station.sid] = station.sl;
// compatibility for Wuxi & Ningbo...
stationsCoordinates[station.si] = station.sl;
});
});
return stationsCoordinates;
};
const getIndex = async (coordinates, type) => {
const data = await (await fetch(`https://restapi.amap.com/v3/place/around` +
`?key=4aa5a510995024236698da8491bd0b81` +
`&location=${coordinates}` +
`&types=${type}` +
`&radius=500` +
`&offset=25`)).json();
return data.count;
};
let subwayInfoGlobal = {};
// int amap api related functions
window.cbk = async () => {
const cityName = getUrlParam('city');
const cityAdcode = await getCityAdcode(cityName);
// document.querySelector('#regular').href = `http://map.amap.com/subway/index.html?&${cityAdcode}`;
const mapMode = location.hash.slice(1);
const subwayInfo = await getSubwayInfo(cityName, cityAdcode);
subwayInfoGlobal = subwayInfo;
const stationsCoordinates = getStationsCoordinates(subwayInfo);
const mySubway = subway("mySubway", {
easy: (mapMode === 'route' ? 1 : 0),
adcode: cityAdcode
});
let scale = 1;
mySubway.event.on("wheel", (e) => {
if (e.deltaY > 0 && scale > 0.51) {
scale -= 0.1;
} else if (e.deltaY < 0 && scale < 1.29) {
scale += 0.1;
}
mySubway.scale(scale);
});
if (mapMode === 'data') {
mySubway.event.on("station.touch", async (event, info) => {
console.log(info)
const id = info.id;
mySubway.stopAnimation();
const indicies = {
officeBuilding: await getIndex(stationsCoordinates[id], '商务写字楼'),
residential: await getIndex(stationsCoordinates[id], '住宅小区'),
touristAttractions: await getIndex(stationsCoordinates[id], '风景名胜'),
commerce: await getIndex(stationsCoordinates[id], '购物中心'),
}
console.log()
mySubway.addInfoWindow(id, {
content: `
<div class="tip_bady">
<div class="tip_name_detail">
<span class="tip_name" id="tip_name">${info.name}</span>
</div>
<div class="tip_route">
<p>坐标: ${stationsCoordinates[id]}</p>
<p>写字楼: ${indicies.officeBuilding}</p>
<p>住宅: ${indicies.residential}</p>
<p>景点: ${indicies.touristAttractions}</p>
<p>商业: ${indicies.commerce}</p>
</div>
</div>
`
});
const center = mySubway.getStCenter(id);
mySubway.setCenter(center);
});
}
if (mapMode === 'traffic') {
const response = await fetch(`data/${cityAdcode}.json`);
if ( response.status === 404) {
document.querySelector('.fetch-data').innerHTML = `Traffic data not avaliable, <a href="#traffic" onclick="fetchTraffic()" >start fetching data</a>`;
} else {
document.querySelector('.fetch-data').innerHTML = `Data avaliable, Generating map`;
window.open(`traffic.html?&${cityAdcode}`);
}
}
}
const fetchTraffic = () => {
document.querySelector('.fetch-data').innerHTML = `Fetching...`;
getTraffic(subwayInfoGlobal);
}
const isSelected = () => {
document.querySelector(`option[value=${location.hash.substr(1)}]`).setAttribute('selected', 'true');
document.querySelector(`option[value=${getUrlParam('city')}]`).setAttribute('selected', 'true');
}
// init functions
isSelected();