Back to PhocaGalleryRenderMaposm class

Method renderRouting

public
renderRouting
(mixed $latFrom = 0, mixed $lngFrom = 0, mixed $latTo = 0, mixed $lngTo = 0, mixed $markerId = '', mixed $markerIconOptions = array(), mixed $language = '')

Method renderRouting - Source code

public function renderRouting($latFrom = 0, $lngFrom = 0, $latTo = 0, $lngTo = 0, $markerId = '', $markerIconOptions = array(), $language = '')
{
    if ($this->router == 0) {
        return false;
    }
    $o = array();
    if ($this->routerserviceurl == '' && $this->maprouterapikey == '') {
        $o[] = 'console.log(\'Routing Error: No router or service url set\')';
        $this->output[] = implode("\n", $o);
        return true;
    }
    $o[] = 'var routingControl = L.Routing.control({';
    $o[] = '   waypoints: [';
    if ($latFrom == 0 && $lngFrom == 0 && $latTo != 0 && $lngTo != 0) {
        $o[] = '      L.latLng(\'\'),';
    } else {
        if ($latFrom == 0 && $lngFrom == 0) {
            $o[] = '      L.latLng(\'\'),';
        } else {
            $o[] = '      L.latLng(' . PhocaGalleryText::filterValue($latFrom, 'number2') . ', ' . PhocaGalleryText::filterValue($lngFrom, 'number2') . '),';
        }
    }
    if ($latTo == 0 && $lngTo == 0) {
        $o[] = '      L.latLng(\'\'),';
    } else {
        $o[] = '      L.latLng(' . PhocaGalleryText::filterValue($latTo, 'number2') . ', ' . PhocaGalleryText::filterValue($lngTo, 'number2') . ')';
    }
    $o[] = '   ],';
    if ($language != '') {
        $o[] = '   language: \'' . PhocaGalleryText::filterValue($language, 'text') . '\',';
    }
    if ($markerId != '') {
        //$o[] = '   marker: marker'.$markerId.',';
        // Don't create new marker for routing (so if we have "TO" address with marker created in map
        // don't display any marker
        //if (!empty($markerIconOptions)) {
        if ($latTo != 0 && $lngTo != 0) {
            $o[] = '   createMarker: function(i,wp, n) {';
            $o[] = '      var latToMarker = ' . PhocaGalleryText::filterValue($latTo, 'number2') . ';';
            $o[] = '      var lngToMarker = ' . PhocaGalleryText::filterValue($lngTo, 'number2') . ';';
            $o[] = '      if (wp.latLng.lat == latToMarker && wp.latLng.lng == lngToMarker) {';
            $o[] = '         return false;';
            $o[] = '      } else {';
            // Get the same icon as the "To" (End) has
            if (!empty($markerIconOptions)) {
                $o[] = '       var ma = L.marker(wp.latLng);';
                $o[] = '       var ic = new L.AwesomeMarkers.icon({';
                foreach ($markerIconOptions as $k => $v) {
                    // Change the icon to circle (e.g. the "To" (End) is set to home, so don't render the same icon for "From" (start) address
                    if (strpos($v, 'icon:') !== false) {
                        $v = 'icon: "circle",';
                    }
                    $o[] = '          ' . $v . "\n";
                }
                $o[] = '       });';
                $o[] = '       ma.setIcon(ic);';
                $o[] = '       return ma;';
            } else {
                $o[] = '         return L.marker(wp.latLng);';
            }
            $o[] = '      }';
            $o[] = '   },';
        }
    }
    $o[] = '   routeWhileDragging: true,';
    $o[] = '   geocoder: L.Control.Geocoder.nominatim(),';
    $o[] = '   reverseWaypoints: true,';
    $o[] = '   showAlternatives: true,';
    $o[] = '   collapsible: true,';
    $o[] = '   show: false,';
    if ($this->routerserviceurl == 'https://api.mapbox.com/directions/v5') {
        // DEBUG DEMO - default address of leaflet-routing-machine to debug
    } else {
        if ($this->routerserviceurl != '') {
            $o[] = '   routerserviceurl: \'' . $this->routerserviceurl . '\',';
        } else {
            if ($this->osm_map_type == 'mapbox' && $this->maprouterapikey != '') {
                $o[] = '   router: L.Routing.mapbox(\'' . PhocaGalleryText::filterValue($this->maprouterapikey) . '\'),';
            } else {
                $o[] = array();
                $o[] = 'console.log(\'Routing Error: No router or service url set\')';
                $this->output[] = implode("\n", $o);
                return true;
            }
        }
    }
    if ($this->routerprofile != '') {
        $o[] = '   profile: \'' . PhocaGalleryText::filterValue($this->routerprofile) . '\',';
    }
    $o[] = '})';
    // $o[] = '.on(\'routingstart\', showSpinner)';
    //$o[] = '.on(\'routesfound routingerror\', hideSpinner)';
    $o[] = '.addTo(map' . $this->name . $this->id . ');';
    //$o[] = 'routingControl.hide();';
    $this->output[] = implode("\n", $o);
    return true;
}