-
Notifications
You must be signed in to change notification settings - Fork 0
/
getdirections.php
73 lines (62 loc) · 2.6 KB
/
getdirections.php
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
<?php
$config = include('config.php');
$key = $config['apikey'];
?>
<html>
<head>
<title>webOS Directions Test</title>
</head>
<body>
<form method="post">
Origin: <input type="text" name="origin" value=""<?php echo (isset($_POST['origin'])?$_POST['origin']:'') ?>"><br>
Destination: <input type="text" name="destination" value=""<?php echo (isset($_POST['destination'])?$_POST['destination']:'') ?>"><br>
Custom API Key: <input type="text" name="key" value=""> (Leave blank to use test key)<br>
<input type="submit" value="Submit">
</form>
<?php
include 'bingmaps-functions.php';
if(isset($_POST['origin']) && isset($_POST['destination']))
{
// Set default map width and height
$mapWidth = 300;
$mapHeight = 300;
// URL of Bing Maps REST Routes API;
$baseURL = "http://dev.virtualearth.net/REST/v1/Routes";
// Set key based on user input
if (isset($_POST['key']) && $_POST['key'] != "") {
$key = $_POST['key'];
}
// construct parameter variables for Routes call
$wayPoint0 = str_ireplace(" ","%20",$_POST['origin']);
$wayPoint1 = str_ireplace(" ","%20",$_POST['destination']);
$optimize = "time";
$routePathOutput = "Points";
$distanceUnit = "km";
$travelMode = "Driving";
// Construct final URL for call to Routes API
$routesURL = $baseURL."/".$travelMode."?wp.0=".$wayPoint0."&wp.1=".$wayPoint1."&optimize=".$optimize."&routePathOutput=".$routePathOutput."&distanceUnit=".$distanceUnit."&output=xml&key=".$key;
// Get output from API and convert to XML element using php_xml
$output = file_get_contents($routesURL);
$response = new SimpleXMLElement($output);
// Extract and print number of routes from response
$numRoutes = $response->ResourceSets->ResourceSet->EstimatedTotal;
echo "Number of routes found: ".$numRoutes."<br>";
// Extract and print route instructions from response
$itinerary = $response->ResourceSets->ResourceSet->Resources->Route->RouteLeg->ItineraryItem;
echo "<ol>";
for ($i = 0; $i < count($itinerary); $i++) {
$instruction = $itinerary[$i]->Instruction;
// While looping, construct the $maneuverPoints array for later use (note casting to double)
$maneuverPoints[$i]->Latitude = (double) $itinerary[$i]->ManeuverPoint->Latitude;
$maneuverPoints[$i]->Longitude = (double) $itinerary[$i]->ManeuverPoint->Longitude;
echo "<li>".$instruction."</li>";
}
echo "</ol>";
}
else
{
echo "<p>Please enter your Bing Maps key and complete all address fields, then click submit.</p>";
}
?>
</body>
</html>