Ignore:
Timestamp:
Jun 1, 2023, 12:18:18 AM (12 months ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
  • Modified: Form types made as separate FormManager package.
  • Fixed: PHP 8.1 support.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/Map/Map.php

    r7 r8  
    11<?php
    2 
    3 class ModuleMap extends AppModule
    4 {
    5   function __construct($System)
    6   {
    7     parent::__construct($System);
    8     $this->Name = 'Map';
    9     $this->Version = '1.0';
    10     $this->Creator = 'Chronos';
    11     $this->License = 'GNU/GPL';
    12     $this->Description = 'Map';
    13     $this->Dependencies = array();
    14   }
    15 
    16   function DoStart()
    17   {
    18   }
    19 
    20   function DoInstall()
    21   {
    22   }
    23 
    24   function DoUninstall()
    25   {
    26   }
    27 
    28   function DoUpgrade()
    29   {
    30 
    31   }
    32 }
    332
    343class Map extends Model
     
    4110  var $Key;
    4211
    43   function __construct(Application $System)
     12  function __construct(Core $System)
    4413  {
    4514    parent::__construct($System);
     
    7039}
    7140
    72 class MapSeznam extends Map
    73 {
    74   function Geolocate($Text)
    75   {
    76     //new SMap.Geocoder(query, odpoved);
    77   }
    7841
    79   function Show()
    80   {
    81     $Output .= '<div id="m" style="height:380px"></div>';
    82     $Output .= '<script type="text/javascript">
    83             var centerMap = SMap.Coords.fromWGS84(14.40, 50.08);
    84 var m = new SMap(JAK.gel("m"), centerMap, 16);
    85 var l = m.addDefaultLayer(SMap.DEF_BASE).enable();
    86 m.addDefaultControls();
    87 
    88 
    89 var nalezeno = function(route) {
    90     var vrstva = new SMap.Layer.Geometry();
    91     m.addLayer(vrstva).enable();
    92 
    93     var coords = route.getResults().geometry;
    94     var cz = m.computeCenterZoom(coords);
    95     m.setCenterZoom(cz[0], cz[1]);
    96     var g = new SMap.Geometry(SMap.GEOMETRY_POLYLINE, null, coords);
    97     vrstva.addGeometry(g);
    98 }
    99 
    100 var coords = [
    101     SMap.Coords.fromWGS84(14.434, 50.084),
    102     SMap.Coords.fromWGS84(16.600, 49.195)
    103 ];
    104 var route = new SMap.Route(coords, nalezeno);
    105             </script>';
    106     $this->System->HeadItems[] = '<script src="//api.mapy.cz/loader.js"></script>
    107         <script>Loader.load()</script>';
    108 
    109     return $Output;
    110   }
    111 }
    112 
    113 class MapGoogle extends Map
    114 {
    115   function Route()
    116   {
    117     $Origins = array();
    118     $Destinations = array();
    119     foreach ($this->Path as $Index => $PathItem)
    120     if ($Index > 0)
    121     {
    122        if ($Index == 1) $Origins[] = $this->Path[$Index - 1];
    123        $Destinations[] = $this->Path[$Index];
    124     }
    125     $URL = 'https://maps.googleapis.com/maps/api/distancematrix/json'.
    126       '?origins='.implode('|', $Origins).'&destinations='.implode('|', $Destinations).'&key='.$this->Key;
    127     $Result = file_get_contents($URL);
    128     $Points = array(array('Destination' => $this->Path[0], 'Distance' => 0, 'Duration' => 0));
    129     $I = 1;
    130     while (strpos($Result, '"distance"') !== false) {
    131       $Distance = trim($this->Parse($Result, '"value" :', '}'));
    132       $Duration = trim($this->Parse($Result, '"value" :', '}'));
    133       $Points[] = array('Destination' => $this->Path[$I], 'Distance' => $Distance, 'Duration' => $Duration);
    134       $I++;
    135     }
    136     return $Points;
    137   }
    138 
    139   function Show()
    140   {
    141     $WayPoints = $this->Path;
    142     array_shift($WayPoints);
    143     array_pop($WayPoints);
    144     if (count($WayPoints) > 0)
    145       $WaypointsQuery = '&waypoints='.implode('|', $WayPoints);
    146       else $WaypointsQuery = '';
    147 
    148     $Output = '<iframe width="'.$this->Width.'" height="'.$this->Height.'" frameborder="0" scrolling="no" '.
    149       'marginheight="0" marginwidth="0" src="'.'https://www.google.com/maps/embed/v1/directions'.
    150       '?key='.$this->Key.'&origin='.$this->Path[0].$WaypointsQuery.
    151       '&destination='.$this->Path[count($this->Path) - 1].''.
    152       '" style="border: 1px solid black"></iframe>';
    153     return $Output;
    154   }
    155 }
    156 
    157 class MapOSM extends Map
    158 {
    159   function Geolocate($Text)
    160   {
    161     // http://wiki.openstreetmap.org/wiki/Nominatim
    162     $URL = 'http://nominatim.openstreetmap.org/search.php?q='.urlencode($Text).'&viewbox=&format=xml&limit=1';
    163     $Result = file_get_contents($URL);
    164     $Result = substr($Result, strpos($Result, 'boundingbox="') + 13);
    165     $BoundingBox = substr($Result, 0, strpos($Result, '"') - 1);
    166     $Parts = explode(',', $BoundingBox);
    167     $Zoom = 1 / ($Parts[1] - $Parts[0]) * 2;
    168     $Result = substr($Result, strpos($Result, 'lat=\'') + 5);
    169     $Latitude = substr($Result, 0, strpos($Result, '\'') - 1);
    170     $Result = substr($Result, strpos($Result, 'lon=\'') + 5);
    171     $Longitude = substr($Result, 0, strpos($Result, '\'') - 1);
    172     return array('Latitude' => $Latitude, 'Longitude' => $Longitude, 'BoundingBox' => $BoundingBox, 'Zoom' => $Zoom);
    173   }
    174 
    175   function Route($TextPathItems)
    176   {
    177     $this->Points = array();
    178     foreach ($TextPathItems as $TextPathItem)
    179     {
    180       $Result = $this->Geolocate($TextPathItem);
    181       $this->Points[] = array('Longitude' => $Result['Longitude'],
    182         'Latitude' => $Result['Latitude']);
    183     }
    184 
    185     $WayPoints = array();
    186     foreach ($this->Points as $Point)
    187     {
    188       $WayPoints[] = $Point['Longitude'];
    189       $WayPoints[] = $Point['Latitude'];
    190     }
    191 
    192     // http://wiki.openstreetmap.org/wiki/OpenRouteService#OpenRouteService_API
    193     $this->Key = 'ee0b8233adff52ce9fd6afc2a2859a28';
    194     $URL = 'http://openls.geog.uni-heidelberg.de/route?api_key='.
    195       $this->Key.'&start='.implode(',', $this->Points[0]).'&end='.implode(',', $this->Points[count($this->Points) - 1]).
    196       '&via='.implode(',', $WayPoints).'&lang=en&distunit=M&routepref=Car&weighting=Fastest&avoidAreas='.
    197       '&useTMC=false&noMotorways=false&noTollways=false&noUnpavedroads=false&noSteps=false'.
    198       '&noFerries=false&elevation=false&surface=false&instructions=false';
    199     $Result = file_get_contents($URL);
    200     echo($Result);
    201     $Separator = '<xls:TotalDistance uom="M" value="';
    202     $Result = substr($Result, strpos($Result, $Separator) + strlen($Separator));
    203     $TotalDistance = substr($Result, 0, strpos($Result, '"/>') - 1);
    204     $Separator = '<xls:TotalTime>PT2H3M48S';
    205     $Result = substr($Result, strpos($Result, $Separator) + strlen($Separator));
    206     $TotalTime = substr($Result, 0, strpos($Result, '"/>') - 1);
    207 
    208     $Points = array();
    209     $Separator = '<gml:pos>';
    210     while (strpos($Result, $Separator) !== false)
    211     {
    212       $Result = substr($Result, strpos($Result, $Separator) + strlen($Separator));
    213       $Pos = substr($Result, 0, strpos($Result, '</gml:pos>') - 1);
    214       $PosParts = explode(' ', $Pos);
    215       $Points[] = array('Longitude' => $PosParts[0], 'Latitude' => $PosParts[1]);
    216     }
    217     return array('TotalDistance' => $TotalDistance, 'TotalTime' => $TotalTime,
    218       'Points' => $Points);
    219   }
    220 
    221   function Show()
    222   {
    223     $WayPoints = array();
    224     foreach ($this->Points as $Point)
    225     {
    226       $WayPoints[] = $Point['Longitude'];
    227       $WayPoints[] = $Point['Latitude'];
    228     }
    229 
    230     $Output = '<iframe width="'.$this->Width.'" height="'.$this->Height.'" frameborder="0" scrolling="no" '.
    231       'marginheight="0" marginwidth="0" src="'.
    232       'http://www.openrouteservice.org/?pos=8.925670599999979,52.28150333376007&zoom='.
    233       $this->Zoom.'&layer=0B00&routeOpt=Car&wp='.implode(',', $WayPoints).
    234       '&lang=en&routeLang=en&distUnit=m&routeWeight=Fastest'.
    235       'style="border: 1px solid black"></iframe>';
    236 /*    $Latitude = $this->Pos['Latitude'];
    237     $Longitude = $this->Pos['Longitude'];
    238     $Zoom = $this->Zoom;
    239     $BoundingBox = array($Longitude - 1 / $Zoom, $Latitude - 1 / $Zoom, $Longitude + 1 / $Zoom, $Latitude + 1 / $Zoom);
    240     $Output = '<iframe width="'.$this->Width.'" height="'.$this->Height.'" frameborder="0" scrolling="no" '.
    241       'marginheight="0" marginwidth="0" src="http://www.openstreetmap.org/export/embed.html'.
    242       '?bbox='.implode(',', $BoundingBox).'&amp;layer=mapnik&amp;marker='.$Latitude.','.$Longitude.'" '.
    243       'style="border: 1px solid black"></iframe>'.
    244       '<p><small><a href="http://www.openstreetmap.org/'.
    245       '?lat='.$Latitude.'&amp;lon='.$Longitude.';zoom='.$Zoom.'&amp;layers=M&amp;mlat='.$Latitude.'&amp;mlon='.$Longitude.'">View Larger Map</a></small></p>';
    246       */
    247     return $Output;
    248   }
    249 }
Note: See TracChangeset for help on using the changeset viewer.