我有一个函数可以让最近的点(农民)到一个特定的点(客户),我在客户周围做了一个与用户有距离的缓冲区,然后我使用 turf.within 来获取这个缓冲区内的所有点(农民) . 如果我在变量中给出特征集合中的坐标数组,它工作正常searchWithin
,但是当我将它作为变量(数组)给出时,在这种情况下,缓冲区的点bufresult
不起作用。
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());
}
});
}
});
}