source: trunk/Modules/Map/MapOSM.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: 4.2 KB
Line 
1<?php
2
3class MapOSM extends Map
4{
5 private array $Points;
6
7 function Geolocate($Text)
8 {
9 // http://wiki.openstreetmap.org/wiki/Nominatim
10 $URL = 'http://nominatim.openstreetmap.org/search.php?q='.urlencode($Text).'&viewbox=&format=xml&limit=1';
11 $Result = file_get_contents($URL);
12 $Result = substr($Result, strpos($Result, 'boundingbox="') + 13);
13 $BoundingBox = substr($Result, 0, strpos($Result, '"') - 1);
14 $Parts = explode(',', $BoundingBox);
15 $Zoom = 1 / ($Parts[1] - $Parts[0]) * 2;
16 $Result = substr($Result, strpos($Result, 'lat=\'') + 5);
17 $Latitude = substr($Result, 0, strpos($Result, '\'') - 1);
18 $Result = substr($Result, strpos($Result, 'lon=\'') + 5);
19 $Longitude = substr($Result, 0, strpos($Result, '\'') - 1);
20 return array('Latitude' => $Latitude, 'Longitude' => $Longitude, 'BoundingBox' => $BoundingBox, 'Zoom' => $Zoom);
21 }
22
23 function Route($TextPathItems)
24 {
25 $this->Points = array();
26 foreach ($TextPathItems as $TextPathItem)
27 {
28 $Result = $this->Geolocate($TextPathItem);
29 $this->Points[] = array('Longitude' => $Result['Longitude'],
30 'Latitude' => $Result['Latitude']);
31 }
32
33 $WayPoints = array();
34 foreach ($this->Points as $Point)
35 {
36 $WayPoints[] = $Point['Longitude'];
37 $WayPoints[] = $Point['Latitude'];
38 }
39
40 // http://wiki.openstreetmap.org/wiki/OpenRouteService#OpenRouteService_API
41 $this->Key = 'ee0b8233adff52ce9fd6afc2a2859a28';
42 $URL = 'http://openls.geog.uni-heidelberg.de/route?api_key='.
43 $this->Key.'&start='.implode(',', $this->Points[0]).'&end='.implode(',', $this->Points[count($this->Points) - 1]).
44 '&via='.implode(',', $WayPoints).'&lang=en&distunit=M&routepref=Car&weighting=Fastest&avoidAreas='.
45 '&useTMC=false&noMotorways=false&noTollways=false&noUnpavedroads=false&noSteps=false'.
46 '&noFerries=false&elevation=false&surface=false&instructions=false';
47 $Result = file_get_contents($URL);
48 echo($Result);
49 $Separator = '<xls:TotalDistance uom="M" value="';
50 $Result = substr($Result, strpos($Result, $Separator) + strlen($Separator));
51 $TotalDistance = substr($Result, 0, strpos($Result, '"/>') - 1);
52 $Separator = '<xls:TotalTime>PT2H3M48S';
53 $Result = substr($Result, strpos($Result, $Separator) + strlen($Separator));
54 $TotalTime = substr($Result, 0, strpos($Result, '"/>') - 1);
55
56 $Points = array();
57 $Separator = '<gml:pos>';
58 while (strpos($Result, $Separator) !== false)
59 {
60 $Result = substr($Result, strpos($Result, $Separator) + strlen($Separator));
61 $Pos = substr($Result, 0, strpos($Result, '</gml:pos>') - 1);
62 $PosParts = explode(' ', $Pos);
63 $Points[] = array('Longitude' => $PosParts[0], 'Latitude' => $PosParts[1]);
64 }
65 return array('TotalDistance' => $TotalDistance, 'TotalTime' => $TotalTime,
66 'Points' => $Points);
67 }
68
69 function Show()
70 {
71 $WayPoints = array();
72 foreach ($this->Points as $Point)
73 {
74 $WayPoints[] = $Point['Longitude'];
75 $WayPoints[] = $Point['Latitude'];
76 }
77
78 $Output = '<iframe width="'.$this->Width.'" height="'.$this->Height.'" frameborder="0" scrolling="no" '.
79 'marginheight="0" marginwidth="0" src="'.
80 'http://www.openrouteservice.org/?pos=8.925670599999979,52.28150333376007&zoom='.
81 $this->Zoom.'&layer=0B00&routeOpt=Car&wp='.implode(',', $WayPoints).
82 '&lang=en&routeLang=en&distUnit=m&routeWeight=Fastest'.
83 'style="border: 1px solid black"></iframe>';
84/* $Latitude = $this->Pos['Latitude'];
85 $Longitude = $this->Pos['Longitude'];
86 $Zoom = $this->Zoom;
87 $BoundingBox = array($Longitude - 1 / $Zoom, $Latitude - 1 / $Zoom, $Longitude + 1 / $Zoom, $Latitude + 1 / $Zoom);
88 $Output = '<iframe width="'.$this->Width.'" height="'.$this->Height.'" frameborder="0" scrolling="no" '.
89 'marginheight="0" marginwidth="0" src="http://www.openstreetmap.org/export/embed.html'.
90 '?bbox='.implode(',', $BoundingBox).'&amp;layer=mapnik&amp;marker='.$Latitude.','.$Longitude.'" '.
91 'style="border: 1px solid black"></iframe>'.
92 '<p><small><a href="http://www.openstreetmap.org/'.
93 '?lat='.$Latitude.'&amp;lon='.$Longitude.';zoom='.$Zoom.'&amp;layers=M&amp;mlat='.$Latitude.'&amp;mlon='.$Longitude.'">View Larger Map</a></small></p>';
94 */
95 return $Output;
96 }
97}
Note: See TracBrowser for help on using the repository browser.