I am trying to make my geoJSON polygon active with option of selection all markers lying inside.
Following this question: https://gis.stackexchange.com/questions/343927/selecting-points-within-geojson-polygon-bounds-using-leaflet
I was considering this example:
http://www.gistechsolutions.com/leaflet/DEMO/Select/SelectPoints3.html
which is very well, but refers to the circle buffor only.
I was trying to make something exactly the same for my geoJSON layer poligons attached.
My code looks as follows:
//MDU planners
gavin = L.geoJSON(Gavin);
loide = L.geoJSON(Loide);
sam = L.geoJSON(Sam);
jordan = L.geoJSON(Jordan);
steve = L.geoJSON(Steve);
danny = L.geoJSON(Danny);
and then:
function ProcessClick(lat,lon){
console.log("You clicked the map at LAT: "+ lat+" and LONG: "+lon );
if (theCircle != undefined) {
map.removeLayer(theCircle);
};
if (theMarker != undefined) {
map.removeLayer(theMarker);
};
if (geojsonLayer != undefined) {
map.removeLayer(geojsonLayer);
};
theMarker = L.marker([lat,lon]).addTo(map); //Add a marker to show where you clicked.
SelectPoints(lat,lon);
theMarker2 = L.marker([lat,lon]).addTo(map); //Add a marker to show where you clicked. // Newmarker added in order to highlight the geoJSON polygon, where the points will be selected
SelectPoints2(lat,lon);
};
Next instead of:
var selPts = [];
function SelectPoints(lat,lon){
var dist = document.getElementById("miles").value;
xy = [lat,lon]; //center point of circle
var theRadius = parseInt(dist) * 1609.34 //1609.34 meters in a mile //dist is a string so it's
convered to an Interger.
selPts.length =0; //Reset the array if selecting new points
job.eachLayer(function (layer) {
layer_lat_long = layer.getLatLng(); // Lat, long of current point as it loops through - layer 1.
distance_from_centerPoint = layer_lat_long.distanceTo(xy); // Distance from our circle marker To current point in meters
if (distance_from_centerPoint <= theRadius && $('#cf').is(":checked")) { // See if meters is within radius, add the to array
selPts.push(layer.feature);
}
})
I added:
var selPts2 = [];
function SelectPoints2(lat,lon){
var jo = jordan //geoJSON layer already attached as per above
xy = [lat,lon]; //center point of circle
var jorange = jo
selPts2.length =0;
jordan.eachLayer(function (layer) {
layer_lat_long = L.geoJson.getLatLng();
selPts.push(layer.feature);
}
})
map.on('click',function(e){
ProcessClick(jordan)
});
But since my console says, that:
Uncaught SyntaxError: Unexpected end of input
I must be totally wrong:
I saw something similar here:
Select one feature of multiple overlapping features (here polygons) on a Leaflet map
and here
Polygons with highlight features in Leaflet
But is not resolved and refers for the specified geoJson polygon features.
I am looking for sth like this:
http://www.gistechsolutions.com/leaflet/DEMO/basic/basic_Poly.html
but instead of popup I want to have all circlemarkers inside the polygon seen in the sidebar as per in the image below:
Is it possible to achieve?
Thank you for any help
EDIT:
According the @Falke Design user I tried the following links:
Determine if a point reside inside a leaflet polygon
Check if a polygon point is inside another in leaflet
and also:
https://turfjs.org/docs/#pointsWithinPolygon
where is a few JSfiddle examples:
http://jsfiddle.net/guspersson/6s1np2n4/
https://jsfiddle.net/4psL2hoo/1/
http://jsfiddle.net/ehpL8fho/14/
unfortunately not working.
I attached the:
function isMarkerInsidePolygon(marker, poly) {
var inside = false;
var x = marker.getLatLng().lat, y = marker.getLatLng().lng;
for (var ii=0;ii<poly.getLatLngs().length;ii++){
var polyPoints = poly.getLatLngs()[ii];
for (var i = 0, j = polyPoints.length - 1; i < polyPoints.length; j =
i++) {
var xi = polyPoints[i].lat, yi = polyPoints[i].lng;
var xj = polyPoints[j].lat, yj = polyPoints[j].lng;
var intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
}
return inside;
};
to my code, but nothing returned, similarily to the JSturf example, attached even as a dummy example.
var points = turf.points([
[-46.6318, -23.5523],
[-46.6246, -23.5325],
[-46.6062, -23.5513],
[-46.663, -23.554],
[-46.643, -23.557]
]);
var searchWithin = turf.polygon([[
[-46.653,-23.543],
[-46.634,-23.5346],
[-46.613,-23.543],
[-46.614,-23.559],
[-46.631,-23.567],
[-46.653,-23.560],
[-46.653,-23.543]
]]);
var ptsWithin = turf.pointsWithinPolygon(points, searchWithin);
Is anywhere some decent working example of this kind of problem?