<?php

class MapOSM extends Map
{
  private array $Points;

  function Geolocate($Text)
  {
    // http://wiki.openstreetmap.org/wiki/Nominatim
    $URL = 'http://nominatim.openstreetmap.org/search.php?q='.urlencode($Text).'&viewbox=&format=xml&limit=1';
    $Result = file_get_contents($URL);
    $Result = substr($Result, strpos($Result, 'boundingbox="') + 13);
    $BoundingBox = substr($Result, 0, strpos($Result, '"') - 1);
    $Parts = explode(',', $BoundingBox);
    $Zoom = 1 / ($Parts[1] - $Parts[0]) * 2;
    $Result = substr($Result, strpos($Result, 'lat=\'') + 5);
    $Latitude = substr($Result, 0, strpos($Result, '\'') - 1);
    $Result = substr($Result, strpos($Result, 'lon=\'') + 5);
    $Longitude = substr($Result, 0, strpos($Result, '\'') - 1);
    return array('Latitude' => $Latitude, 'Longitude' => $Longitude, 'BoundingBox' => $BoundingBox, 'Zoom' => $Zoom);
  }

  function Route($TextPathItems)
  {
    $this->Points = array();
    foreach ($TextPathItems as $TextPathItem)
    {
      $Result = $this->Geolocate($TextPathItem);
      $this->Points[] = array('Longitude' => $Result['Longitude'],
        'Latitude' => $Result['Latitude']);
    }

    $WayPoints = array();
    foreach ($this->Points as $Point)
    {
      $WayPoints[] = $Point['Longitude'];
      $WayPoints[] = $Point['Latitude'];
    }

    // http://wiki.openstreetmap.org/wiki/OpenRouteService#OpenRouteService_API
    $this->Key = 'ee0b8233adff52ce9fd6afc2a2859a28';
    $URL = 'http://openls.geog.uni-heidelberg.de/route?api_key='.
      $this->Key.'&start='.implode(',', $this->Points[0]).'&end='.implode(',', $this->Points[count($this->Points) - 1]).
      '&via='.implode(',', $WayPoints).'&lang=en&distunit=M&routepref=Car&weighting=Fastest&avoidAreas='.
      '&useTMC=false&noMotorways=false&noTollways=false&noUnpavedroads=false&noSteps=false'.
      '&noFerries=false&elevation=false&surface=false&instructions=false';
    $Result = file_get_contents($URL);
    echo($Result);
    $Separator = '<xls:TotalDistance uom="M" value="';
    $Result = substr($Result, strpos($Result, $Separator) + strlen($Separator));
    $TotalDistance = substr($Result, 0, strpos($Result, '"/>') - 1);
    $Separator = '<xls:TotalTime>PT2H3M48S';
    $Result = substr($Result, strpos($Result, $Separator) + strlen($Separator));
    $TotalTime = substr($Result, 0, strpos($Result, '"/>') - 1);

    $Points = array();
    $Separator = '<gml:pos>';
    while (strpos($Result, $Separator) !== false)
    {
      $Result = substr($Result, strpos($Result, $Separator) + strlen($Separator));
      $Pos = substr($Result, 0, strpos($Result, '</gml:pos>') - 1);
      $PosParts = explode(' ', $Pos);
      $Points[] = array('Longitude' => $PosParts[0], 'Latitude' => $PosParts[1]);
    }
    return array('TotalDistance' => $TotalDistance, 'TotalTime' => $TotalTime,
      'Points' => $Points);
  }

  function Show()
  {
    $WayPoints = array();
    foreach ($this->Points as $Point)
    {
      $WayPoints[] = $Point['Longitude'];
      $WayPoints[] = $Point['Latitude'];
    }

    $Output = '<iframe width="'.$this->Width.'" height="'.$this->Height.'" frameborder="0" scrolling="no" '.
      'marginheight="0" marginwidth="0" src="'.
      'http://www.openrouteservice.org/?pos=8.925670599999979,52.28150333376007&zoom='.
      $this->Zoom.'&layer=0B00&routeOpt=Car&wp='.implode(',', $WayPoints).
      '&lang=en&routeLang=en&distUnit=m&routeWeight=Fastest'.
      'style="border: 1px solid black"></iframe>';
/*    $Latitude = $this->Pos['Latitude'];
    $Longitude = $this->Pos['Longitude'];
    $Zoom = $this->Zoom;
    $BoundingBox = array($Longitude - 1 / $Zoom, $Latitude - 1 / $Zoom, $Longitude + 1 / $Zoom, $Latitude + 1 / $Zoom);
    $Output = '<iframe width="'.$this->Width.'" height="'.$this->Height.'" frameborder="0" scrolling="no" '.
      'marginheight="0" marginwidth="0" src="http://www.openstreetmap.org/export/embed.html'.
      '?bbox='.implode(',', $BoundingBox).'&amp;layer=mapnik&amp;marker='.$Latitude.','.$Longitude.'" '.
      'style="border: 1px solid black"></iframe>'.
      '<p><small><a href="http://www.openstreetmap.org/'.
      '?lat='.$Latitude.'&amp;lon='.$Longitude.';zoom='.$Zoom.'&amp;layers=M&amp;mlat='.$Latitude.'&amp;mlon='.$Longitude.'">View Larger Map</a></small></p>';
      */
    return $Output;
  }
}
