Beginner Beginner - 1 year ago 55
Ajax Question

Turf.within is not working

I have a function to get the nearest points (farmers) to a specific point(customer),i made a buffer around the customer with distance from the user then i am using turf.within to get all the points(farmers) inside this buffer.
It works fine if i give the array of coordinates in the feature collection in the variable

,but when i gave it as a variable (array) which is in this case the points of the buffer
it doesn't work.

function Find_Nearest()
//getting the orderid to get the customer id
var select1 = document.getElementById('OrderId');
var Order_ID = select1.options[select1.selectedIndex].text;
var Cust_Name = document.getElementById('CustName').textContent;

//to check the distance entered by user
var select = document.querySelector('input[name="optradio"]:checked').value;
var dist=document.getElementById('dist').value;

if (select == "EnterRange")
if (dist == null || dist == "" || isNaN(dist) )
alert("Please enter a valid range");
document.getElementById('dist').focus() ;
else { distance=dist;}
else { distance=select;}

data:{'Cust_Name': Cust_Name} ,
dataType: 'json',
success: function (response)
var unit = 'kilometers';
var buffered = turf.buffer(response, distance, unit);
var bufresult = new Array();

type: "POST",
url: 'allfarmers_geojson.php',
dataType: 'json',
success: function (data)
var searchWithin = {
"type": "FeatureCollection",
"features": [
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": bufresult //the problem is here
// [[
// [126.5443329669793,8.71772],
// [126.17866648348965,9.34375583885896],
// [125.44733351651035,9.34375583885896],
// [125.08166703302071,8.71772],
// [125.44733351651035,8.091684161141039],
// [126.17866648348965,8.091684161141039],
// [126.5443329669793,8.71772]
// ]]


var ptsWithin = turf.within(data, searchWithin);
geojsonLayer = L.geoJson(ptsWithin).addTo(mymap);

Answer Source

Have you tried replacing

var bufresult = new Array(); and bufresult.push(buffered.geometry.coordinates);


bufresult = buffered.geometry.coordinates

I think the issue may be that you are initializing an array (bufresult) and then pushing the buffered geometry coordinates into that array. You could actually just skip the bufresult variable all together and just plug the buffered.geometry.coordinates variable into where you're using bufresult. Let me know if this doesn't work for you. Could you also include the error that you're getting?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download