Dispaying a route with WrldRouteView
Using the WrldNavigation
widget to find a route and display that route using the WrldRouteView
widget.
<!DOCTYPE HTML>
<html>
<head>
<script src="https://unpkg.com/wrld.js@1.x.x"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.1/leaflet.css" rel="stylesheet" />
<link href="https://cdn-webgl.wrld3d.com/wrldjs/addons/resources/v1/latest/css/wrld.css" rel="stylesheet"/>
<script src="https://cdn-webgl.wrld3d.com/wrldjs/addons/navigation/v1/latest/navigation.js"></script>
<script src="https://cdn-webgl.wrld3d.com/wrldjs/addons/routeview/v1/latest/routeview.js"></script>
</head>
<body>
<div style="position: relative">
<div id="map" style="height: 500px"></div>
<div style="position: absolute; top: 0; left: 0; display: flex; flex-direction: column; z-index: 20;">
<button onclick="findRoute()">Find Route</button>
<button onclick="clearRoute()">Clear Route</button>
</div>
<script>
var apiKey = "your_api_key_here";
var map = Wrld.map("map", apiKey, {
center: [56.461762, -2.975963],
zoom: 16
});
var navigation = new WrldNavigation(null, map, apiKey);
var routeView = new WrldRouteView(map);
function clearRoute() {
routeView.clearDirections();
}
function findRoute() {
var startLocation = WrldNavigation.buildLocation("Westport House", 56.4600344, -2.9783117);
var endLocation = WrldNavigation.buildLocation("Abertay", 56.463008, -2.974135);
navigation.findRoute(startLocation, endLocation, findRouteCallback);
}
function findRouteCallback(result) {
if (result.route) {
navigation.buildDirectionsForRoute(result.route, buildDirectionsCallback);
}
}
function buildDirectionsCallback(result) {
if (result.directions) {
routeView.setDirections(result.directions);
}
}
</script>
</div>
</body>
</html>