javascriptdictionaryleafletleaflet-routing-machine

verify if marker is inside from route


my name is João, and I'm using the leaflet.js to do my project of the conclusion course at the University of Campinas - Brazil. The problem to solve is: the marker needs to stay inside from route, if not, need to return a false. Can someone help me? Thanks.

var map = L.map('map',18);

  L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    maxZoom: 20,
  }).addTo(map);

  var cont = 0;
  var marker = null;

  function refresh() {

    $.ajax({
      method: "GET",
      url: "mapa/coordenada",
      dataType: "json"
    })
      .done(function( msg ) {
        registro_coordenada(msg.coordenada);
      });
  }

  function select_cord() {

    $.ajax({
      method: "GET",
      url: "mapa/coordenada",
      dataType: "json"
    })
      .done(function( msg ) {
        add_cord(msg.coordenada);
      });
  }

  function add_cord(coordenada){
    out = coordenada.split(",");

    var control = L.Routing.control(L.extend(window.lrmConfig, {
      waypoints: [
      L.latLng(-22.5627235,-47.425501),
      L.latLng([out[0],out[1]])
  ],
      geocoder: L.Control.Geocoder.nominatim(),
      routeWhileDragging: true,
      reverseWaypoints: true,
      showAlternatives: true,
      altLineOptions: {
          styles: [
            {color: 'black', opacity: 0.15, weight: 9},
            {color: 'white', opacity: 0.8, weight: 6},
            {color: 'blue', opacity: 0.5, weight: 2}
        ]
    }
  })).addTo(map);

  L.Routing.errorControl(control).addTo(map);

  }

var icone = new L.Icon({
                      iconUrl: 'https://cdn.rawgit.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-red.png',
                      shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/images/marker-shadow.png',
                      iconSize: [25, 41],
                      iconAnchor: [12, 41],
                      popupAnchor: [1, -34],
                      shadowSize: [41, 41]
                    });


function registro_coordenada(coordenada){
  out = coordenada.split(",");
  if (cont === 0 ){
    marker = L.marker([out[0],out[1]], {icon: icone}).addTo(map).bindPopup("<b>Localização atual</b>").openPopup();
    cont = 1;
    return;
  }
  map.removeLayer(marker);
  //var marker = L.marker([out[0],out[1]]).remove(map).bindPopup("<b>Localização atual</b>").openPopup();
  marker = L.marker([out[0],out[1]], {icon: icone}).addTo(map).bindPopup("<b>Localização atual</b>").openPopup().closePopup();
}

$(document).ready(function(){
  if ($('#map')){
    self.setInterval(function () {
      refresh()
    }, 1000);
  }
  select_cord();
});

The problem can't be solved at GitHub

If someone helps me, I'll grateful so much!


Solution

  • You can't do this, because of how the finer details of how computational geometry works: Because the points of a Linestring are represented in floating point, there can be line segments for which there are no (valid) floating point representation of the points along that segment.

    The approach to take here is to calculate the distance from a given Point to a given Linestring, then check if that distance is within a threshold. So the meaning of "inside" changes, becoming "no more than (threshold) distance apart from the line".

    Note that any point-to-line distance algorithm might return zero for points that are not on the line, due to floating point rounding errors (in those cases, the distance from the point to the line is less than the precision of the floating point format in use or the floating point errors accumulated during the calculations).

    The most well-known (to my knowledge) javascript implementation of the distance-from-point-to-linestring algorithm is the one in Turf.js.