Turf.within 不工作
Turf.within is not working
我有一个函数可以让最近的点(农民)到一个特定的点(客户),我在客户周围做了一个缓冲区,距离用户有一定距离,然后我使用 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());
}
});
}
});
}
你试过更换
var bufresult = new Array();
和
bufresult.push(buffered.geometry.coordinates);
和
bufresult = buffered.geometry.coordinates
我认为问题可能在于您正在初始化一个数组 (bufresult
),然后将缓冲的几何坐标推入该数组。实际上,您可以完全跳过 bufresult
变量,而只需将 buffered.geometry.coordinates
变量插入您使用 bufresult
的位置。如果这对您不起作用,请告诉我。您能否也包括您遇到的错误?
我有一个函数可以让最近的点(农民)到一个特定的点(客户),我在客户周围做了一个缓冲区,距离用户有一定距离,然后我使用 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());
}
});
}
});
}
你试过更换
var bufresult = new Array();
和
bufresult.push(buffered.geometry.coordinates);
和
bufresult = buffered.geometry.coordinates
我认为问题可能在于您正在初始化一个数组 (bufresult
),然后将缓冲的几何坐标推入该数组。实际上,您可以完全跳过 bufresult
变量,而只需将 buffered.geometry.coordinates
变量插入您使用 bufresult
的位置。如果这对您不起作用,请告诉我。您能否也包括您遇到的错误?