Skip to content

Commit 05c5a0b

Browse files
authored
Create static-map-direction.php
1 parent 178510f commit 05c5a0b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

static-map-direction.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public function getStaticGmapURLForDirection($origin, $destination, $waypoints, $size = "500x500") {
2+
$markers = array();
3+
$waypoints_labels = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K");
4+
$waypoints_label_iter = 0;
5+
6+
$markers[] = "markers=color:green" . urlencode("|") . "label:" . urlencode($waypoints_labels[$waypoints_label_iter++] . '|' . $origin);
7+
foreach ($waypoints as $waypoint) {
8+
$markers[] = "markers=color:blue" . urlencode("|") . "label:" . urlencode($waypoints_labels[$waypoints_label_iter++] . '|' . $waypoint);
9+
}
10+
$markers[] = "markers=color:red" . urlencode("|") . "label:" . urlencode($waypoints_labels[$waypoints_label_iter] . '|' . $destination);
11+
12+
$url = "https://maps.googleapis.com/maps/api/directions/json?origin=$origin&destination=$destination&waypoints=" . implode($waypoints, '|');
13+
$ch = curl_init();
14+
curl_setopt($ch, CURLOPT_URL, $url);
15+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
16+
curl_setopt($ch, CURLOPT_POST, false);
17+
$result = curl_exec($ch);
18+
curl_close($ch);
19+
$googleDirection = json_decode($result, true);
20+
21+
$polyline = urlencode($googleDirection['routes'][0]['overview_polyline']['points']);
22+
$markers = implode($markers, '&');
23+
24+
return "https://maps.googleapis.com/maps/api/staticmap?size=$size&maptype=roadmap&path=enc:$polyline&$markers";
25+
}

0 commit comments

Comments
 (0)