-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
156 lines (140 loc) · 4.33 KB
/
index.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
149
150
151
152
153
154
155
156
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>Hello, World</title>
<style>
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#container {
height: 80%;
overflow: hidden;
margin: 0;
font-family: '微软雅黑';
}
</style>
<script
type="text/javascript"
src="https://api.map.baidu.com/api?v=1.0&type=webgl&ak=2piv3H0K22WXzqgwtGwYusO3lWzGRrNw"
></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.16.9/xlsx.full.min.js"></script>
</head>
<body>
<div id="container"></div>
<script>
var map = new BMapGL.Map('container')
var myGeo = new BMapGL.Geocoder()
var zoomCtrl = new BMapGL.ZoomControl()
map.addControl(zoomCtrl)
map.centerAndZoom(new BMapGL.Point(116.331398, 39.897445), 12)
var columnG = []
// Function to fetch and process the Excel file
async function fetchAndProcessExcel() {
console.log('fetching')
try {
const response = await fetch('SAS Bus Route.xlsx')
const data = await response.arrayBuffer()
const workbook = XLSX.read(data, { type: 'array' })
// Assuming the first sheet is the one you're interested in
const sheetName = workbook.SheetNames[0]
const worksheet = workbook.Sheets[sheetName]
// Extract data from column G (ignoring the header)
for (let i = 2; ; i++) {
const cellAddress = 'E' + i
const cellAddress2 = 'G' + i
if (!worksheet[cellAddress] || !worksheet[cellAddress2]) break
columnG.push(worksheet[cellAddress].v + ' | ' + worksheet[cellAddress2].v)
}
columnG.push('school') // school address
console.log(columnG)
// After fetching and processing, proceed with the rest of the code
processData()
} catch (error) {
console.error('Error fetching or processing the Excel file:', error)
}
}
// Call the function on window load
window.onload = fetchAndProcessExcel
var min_lng = 999
var min_lat = 999
var max_lng = 0
var max_lat = 0
var promises = []
var full_locations = []
function processData() {
for (var i = 0; i < columnG.length; i++) {
;(function (index) {
var promise = new Promise(function (resolve, reject) {
myGeo.getPoint(
columnG[index],
function (point) {
if (point) {
if (point.lng > max_lng) max_lng = point.lng
if (point.lat > max_lat) max_lat = point.lat
if (min_lng > point.lng) min_lng = point.lng
if (min_lat > point.lat) min_lat = point.lat
var loc = [point, columnG[index]] // lng lat
full_locations.push(loc)
map.addOverlay(new BMapGL.Marker(point, { title: columnG[index] }))
var label = new BMapGL.Label(columnG[index], {
position: point,
offset: new BMapGL.Size(10, 20),
})
map.addOverlay(label)
resolve()
} else {
console.log('您选择的地址没有解析到结果!')
reject()
}
},
'上海市'
)
})
promises.push(promise)
})(i)
}
Promise.all(promises)
.then(function () {
var lat = min_lat + (max_lat - min_lat) / 2
var lng = min_lng + (max_lng - min_lng) / 2
console.log(lng)
console.log(lat)
var center_pt = new BMapGL.Point(lng, lat)
map.centerAndZoom(center_pt, 12)
var driving = new BMapGL.DrivingRoute(map, {
onSearchComplete: function (results) {
if (driving.getStatus() == BMAP_STATUS_SUCCESS) {
var plan = results.getPlan(0)
var distance = plan.getDistance(true) // Get distance in meters
var duration = plan.getDuration(true)
console.log(distance)
console.log(duration)
} else {
console.log('Unable to calculate distance.')
}
},
})
var x = driving.search(full_locations[0][0], full_locations[2][0], full_locations[1][0])
console.log(x)
console.log(
'that is the dist btw ' +
full_locations[0][1] +
' and ' +
full_locations[1][1] +
' and ' +
full_locations[2][1]
)
})
.catch(function () {
console.log('Some geocoding requests failed.')
})
}
</script>
<a href="index2.html">index2</a>
</body>
</html>