alex3165 / react-leaflet-draw

React component for leaflet-draw on top of react-leaflet
224 stars 151 forks source link

Checking markers inside of polygon is inaccurate #124

Open acepitcher2009 opened 3 years ago

acepitcher2009 commented 3 years ago

My team and i have been working with react leaflet draw for the past few months on a project and we noticed that when checking for markers inside of a polygon the accuracy is incorrect. I have tried to find a work around to this problem but have failed. What is happening is when i select 2 out of the 43 markers on the map dispatch the results to redux, the state is showing to have 3 markers selected instead of just the two that are selected. It also shows more than just one extra selected marker at times when selecting more than two. If you could get this fixed soon it would be greatly appreciated because we are wanting to use this package in our new project as well. i have also tried to use the point in polygon package for this issue using the latlngs instead of bounds but when using the .contains function the project errors out

image image image

jorgenboganes commented 2 years ago

Just do something like

  export function pointIsInPolygon(point: LatLng, polygon: LatLngTuple[]) {
      var x = point.lng,
          y = point.lat;

      var inside = false;
      for (var i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
          var xi = polygon[i][0],
              yi = polygon[i][1];
          var xj = polygon[j][0],
              yj = polygon[j][1];
          var intersect =
              yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
          if (intersect) inside = !inside;
      }
      return inside;
  }