source: trunk/Modules/Map/MapGoogle.php

Last change on this file was 8, checked in by chronos, 19 months ago
  • Modified: Updated Common package.
  • Modified: Form types made as separate FormManager package.
  • Fixed: PHP 8.1 support.
File size: 1.6 KB
Line 
1<?php
2
3class MapGoogle extends Map
4{
5 function Route()
6 {
7 $Origins = array();
8 $Destinations = array();
9 foreach ($this->Path as $Index => $PathItem)
10 if ($Index > 0)
11 {
12 if ($Index == 1) $Origins[] = $this->Path[$Index - 1];
13 $Destinations[] = $this->Path[$Index];
14 }
15 $URL = 'https://maps.googleapis.com/maps/api/distancematrix/json'.
16 '?origins='.implode('|', $Origins).'&destinations='.implode('|', $Destinations).'&key='.$this->Key;
17 $Result = file_get_contents($URL);
18 $Points = array(array('Destination' => $this->Path[0], 'Distance' => 0, 'Duration' => 0));
19 $I = 1;
20 while (strpos($Result, '"distance"') !== false) {
21 $Distance = trim($this->Parse($Result, '"value" :', '}'));
22 $Duration = trim($this->Parse($Result, '"value" :', '}'));
23 $Points[] = array('Destination' => $this->Path[$I], 'Distance' => $Distance, 'Duration' => $Duration);
24 $I++;
25 }
26 return $Points;
27 }
28
29 function Show()
30 {
31 $WayPoints = $this->Path;
32 array_shift($WayPoints);
33 array_pop($WayPoints);
34 if (count($WayPoints) > 0)
35 $WaypointsQuery = '&waypoints='.implode('|', $WayPoints);
36 else $WaypointsQuery = '';
37
38 $Output = '<iframe width="'.$this->Width.'" height="'.$this->Height.'" frameborder="0" scrolling="no" '.
39 'marginheight="0" marginwidth="0" src="'.'https://www.google.com/maps/embed/v1/directions'.
40 '?key='.$this->Key.'&origin='.$this->Path[0].$WaypointsQuery.
41 '&destination='.$this->Path[count($this->Path) - 1].''.
42 '" style="border: 1px solid black"></iframe>';
43 return $Output;
44 }
45}
Note: See TracBrowser for help on using the repository browser.