问题
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 searchWithin
,but when i gave it as a variable (array) which is in this case the points of the buffer bufresult
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;}
$.ajax({
type:"POST",
url:"CustomerID_geojson.php",
data:{'Cust_Name': Cust_Name} ,
dataType: 'json',
success: function (response)
{
var unit = 'kilometers';
var buffered = turf.buffer(response, distance, unit);
var bufresult = new Array();
bufresult.push(buffered.geometry.coordinates);
$.ajax({
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]
// ]]
}
}
]
};
console.log(bufresult);
console.log(searchWithin);
var ptsWithin = turf.within(data, searchWithin);
console.log(ptsWithin);
geojsonLayer = L.geoJson(ptsWithin).addTo(mymap);
mymap.fitBounds(geojsonLayer.getBounds());
}
});
}
});
}
回答1:
Have you tried replacing
var bufresult = new Array();
and
bufresult.push(buffered.geometry.coordinates);
with
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?
来源:https://stackoverflow.com/questions/40845108/turf-within-is-not-working