-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
198 lines (197 loc) · 7.51 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!DOCTYPE html>
<!-- Street View Path Finder - Demo -->
<!-- Version: 0.1.0(084)-SNAPSHOT -->
<!-- Author: Klnsyf Sun, Chen Xuanlin -->
<!-- Date: 2017-07-09 -->
<!-- hack.init() -->
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Street View Path Finder - Demo</title>
<style>
#map {
height: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
.controls {
margin-top: 10px;
border: 1px solid transparent;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
height: 32px;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
#start,
#end {
background-color: #fff;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
margin-left: 12px;
padding: 0 11px 0 13px;
text-overflow: ellipsis;
width: 400px;
}
#start:focus,
#end:focus {
border-color: #4d90fe;
}
#warnings-panel {
width: 100%;
height:10%;
text-align: center;
}
</style>
</head>
<input id="start" class="controls" type="text" placeholder="Enter an origin location">
<input id="end" class="controls" type="text" placeholder="Enter a destination location">
<div id="map"></div>
<div id="warnings-panel"></div>
<script>
function initMap() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
map.setCenter(pos);
}, function() {
});
} else {
alert("Browser doesn't support Geolocation");
}
var markerArray = [];
var directionsService = new google.maps.DirectionsService;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 13,
center: {lat: 9.1734, lng: -1.1037}
// [Laugh] By Klnsyf Sun
});
var directionsDisplay = new google.maps.DirectionsRenderer({map: map});
var stepDisplay = new google.maps.InfoWindow;
var onChangeHandler = function() {
if(document.getElementById('start').value != 0 && document.getElementById('end').value != 0){
calculateAndDisplayRoute(
directionsDisplay, directionsService, markerArray, stepDisplay, map);
}
};
document.getElementById('start').addEventListener('change', onChangeHandler);
document.getElementById('end').addEventListener('change', onChangeHandler);
new AutocompleteDirectionsHandler(map);
}
function calculateAndDisplayRoute(directionsDisplay, directionsService,
markerArray, stepDisplay, map) {
for (var i = 0; i < markerArray.length; i++) {
markerArray[i].setMap(null);
}
// 路径生成
directionsService.route({
origin: document.getElementById('start').value,
destination: document.getElementById('end').value,
travelMode: 'WALKING'
}, function(response, status) {
if (status === 'OK') {
document.getElementById('warnings-panel').innerHTML =
'<b>' + response.routes[0].warnings + '</b>';
directionsDisplay.setDirections(response);
showSteps(response, markerArray, stepDisplay, map);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
function showSteps(directionResult, markerArray, stepDisplay, map) {
// 遍历检索路径中的转折点
var myRoute = directionResult.routes[0].legs[0];
for (var i = 0; i < myRoute.steps.length; i++) {
var marker = markerArray[i] = markerArray[i] || new google.maps.Marker;
marker.setMap(map);
marker.setPosition(myRoute.steps[i].start_location);
var contentString = '<figure>'+'<img src="https://maps.googleapis.com/maps/api/streetview?size=600x300&location='+marker.position.lat()+','+(Math.round(marker.position.lng()*1000000))/1000000+'&heading=0&pitch=0&scale=2&key=AIzaSyAdUVFJH06fMUerZm1mLBzNBnpuAgYl-vA"/>'+'</figure>';
attachInstructionText(
stepDisplay, marker, contentString,map);
}
}
function attachInstructionText(stepDisplay, marker, text,map) {
google.maps.event.addListener(marker, 'click', function() {
// 点击Marker以显示当前位置的Street View
stepDisplay.setContent(text);
stepDisplay.open(map, marker);
});
}
function AutocompleteDirectionsHandler(map) {
// 变量及监听器初始化
this.map = map;
this.originPlaceId = null;
this.destinationPlaceId = null;
this.travelMode = 'WALKING';
var originInput = document.getElementById('start');
var destinationInput = document.getElementById('end');
this.directionsService = new google.maps.DirectionsService;
this.directionsDisplay = new google.maps.DirectionsRenderer;
this.directionsDisplay.setMap(map);
var originAutocomplete = new google.maps.places.Autocomplete(
originInput, {placeIdOnly: true});
var destinationAutocomplete = new google.maps.places.Autocomplete(
destinationInput, {placeIdOnly: true});
this.setupPlaceChangedListener(originAutocomplete, 'ORIG');
this.setupPlaceChangedListener(destinationAutocomplete, 'DEST');
this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(originInput);
this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(destinationInput);
}
AutocompleteDirectionsHandler.prototype.setupClickListener = function(id, mode) {
var radioButton = document.getElementById(id);
var me = this;
radioButton.addEventListener('click', function() {
me.travelMode = mode;
me.route();
});
};
AutocompleteDirectionsHandler.prototype.setupPlaceChangedListener = function(autocomplete, mode) {
var me = this;
autocomplete.bindTo('bounds', this.map);
autocomplete.addListener('place_changed', function() {
var place = autocomplete.getPlace();
if (!place.place_id) {
window.alert("Please select an option from the dropdown list.");
return;
}
if (mode === 'ORIG') {
me.originPlaceId = place.place_id;
} else {
me.destinationPlaceId = place.place_id;
}
me.route();
});
};
AutocompleteDirectionsHandler.prototype.route = function() {
if (!this.originPlaceId || !this.destinationPlaceId) {
return;
}
var me = this;
this.directionsService.route({
origin: {'placeId': this.originPlaceId},
destination: {'placeId': this.destinationPlaceId},
travelMode: this.travelMode
}, function(response, status) {
if (status === 'OK') {
me.directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
};
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAdUVFJH06fMUerZm1mLBzNBnpuAgYl-vA&libraries=places&callback=initMap">
</script>
</body>
</html>