This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Search Along a Route.html
174 lines (145 loc) · 6.23 KB
/
Search Along a Route.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Along a Route Search - Bing Maps Samples</title>
<meta charset="utf-8" />
<link rel="shortcut icon" href="/favicon.ico"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="This example shows how to search for locations along a route." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, Bing, Bing Maps" />
<meta name="author" content="Microsoft Bing Maps" />
<meta name="screenshot" content="bmv8-findalongrouteexample.jpg" />
<script>
var map, infobox, dataLayer, directionsManager;
//Query URL to the Fourth Coffe Shop data source
var sdsDataSourceUrl = 'https://spatial.virtualearth.net/REST/v1/data/20181f26d9e94c81acdf9496133d4f23/FourthCoffeeSample/FourthCoffeeShops';
function GetMap()
{
map = new Microsoft.Maps.Map('#myMap', {});
//Create a layer for rendering the data that is along a route.
dataLayer = new Microsoft.Maps.Layer();
//Add the layer to the map.
map.layers.insert(dataLayer);
//Add click event to shapes in the data layer.
Microsoft.Maps.Events.addHandler(dataLayer, 'click', shapeClicked);
//Create an infobox at the center of the map but don't show it.
infobox = new Microsoft.Maps.Infobox(map.getCenter(), {
visible: false
});
//Assign the infobox to a map instance.
infobox.setMap(map);
//Load the directions and spatial data service modules.
Microsoft.Maps.loadModule(['Microsoft.Maps.Directions', 'Microsoft.Maps.SpatialDataService'], function () {
//Create an instance of the directions manager.
directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
//Specify where to display the route instructions.
directionsManager.setRenderOptions({ itineraryContainer: '#directionsItinerary' });
//Specify the where to display the input panel
directionsManager.showInputPanel('directionsPanel');
//Add event handler to directions manager.
Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', directionsUpdated);
});
}
function directionsUpdated(e) {
dataLayer.clear();
//Get the current route.
var currentRoute = directionsManager.getCurrentRoute();
if (!currentRoute) {
alert('No route found.');
return;
}
var routeRequest = directionsManager.getRequestOptions();
//Get the route mode.
var routeMode = getRouteMode(routeRequest);
if(!routeMode){
alert('Transit mode is not supported for near route queries.');
return;
}
//Create a query to get data that is near a route.
var queryOptions = {
queryUrl: sdsDataSourceUrl,
spatialFilter: {
spatialFilterType: 'nearRoute',
start: currentRoute.routeLegs[0].startWaypointLocation,
end: currentRoute.routeLegs[0].endWaypointLocation,
travelMode: routeMode,
optimize: getRouteOptimization(routeRequest)
}
};
//Process the query.
Microsoft.Maps.SpatialDataService.QueryAPIManager.search(queryOptions, map, function (data) {
//Add results to the map.
dataLayer.add(data);
});
}
function getRouteMode(routeRequest) {
switch(routeRequest.routeMode){
case Microsoft.Maps.Directions.RouteMode.driving:
return 'Driving';
case Microsoft.Maps.Directions.RouteMode.walking:
return 'Walking';
}
return null;
}
function getRouteOptimization(routeRequest) {
switch(routeRequest.routeOptimize){
case Microsoft.Maps.Directions.RouteOptimization.timeWithTraffic:
return 'timeWithTraffic';
case Microsoft.Maps.Directions.RouteOptimization.shortestDistance:
return 'distance';
case Microsoft.Maps.Directions.RouteOptimization.shortestTime:
default:
return 'time';
}
}
function shapeClicked(e) {
//Make sure the infobox has metadata to display.
if (e.primitive.metadata) {
//Set the infobox options with the metadata of the pushpin.
infobox.setOptions({
location: e.primitive.getLocation(),
title: e.primitive.metadata.Name,
description: 'Store Type: ' + e.primitive.metadata.StoreType,
visible: true
});
}
}
</script>
<style>
html, body {
padding: 0;
margin: 0;
height: 100%;
}
.directionsContainer {
width: 380px;
height: 100%;
overflow-y: auto;
float: left;
}
#myMap {
position: relative;
width: calc(100% - 380px);
height: 100%;
float: left;
}
</style>
</head>
<body>
<div class="directionsContainer">
<div id="directionsPanel"></div>
<div id="directionsItinerary"></div>
</div>
<div id="myMap"></div>
<script>
// Dynamic load the Bing Maps Key and Script
// Get your own Bing Maps key at https://www.microsoft.com/maps
(async () => {
let script = document.createElement("script");
let bingKey = await fetch("https://samples.azuremaps.com/api/GetBingMapsKey").then(r => r.text()).then(key => { return key });
script.setAttribute("src", `https://www.bing.com/api/maps/mapcontrol?callback=GetMap&key=${bingKey}`);
document.body.appendChild(script);
})();
</script>
</body>
</html>