1

我已经使用 geojson api 加载了点数据

    var incidences = new L.GeoJSON.AJAX("http://127.0.0.1:8000/incidence_data/",{

         onEachFeature: function(feature,layer){
            //console.log(feature.properties);
            layer.bindPopup(feature.properties.name.toString())
        }
    });
    incidences.addTo(map);

现在我正在尝试在地图上创建一个按钮,当我单击该按钮时,事件会缓冲一英里。

html

<div id="map2" class="col-md-4 well">
    <p>GeoDjango is da bomb</p>
    <button id="buffer" class="form-control btn-warning">Buffer</button>
</div>

JS

    var bufferLayer;
    var incidences2 = incidences.toGeoJSON();

    $("#buffer").click(function(){
        if ($("#buffer").html()=='Buffer'){
            var buff=turf.buffer(incidences2,1,'miles');
            bufferLayer=L.geoJSON(buff).addTo(map);
            $("#buffer").html("Remove Buffer");
      } else{
            map.removeLayer(bufferLayer);
            $("#buffer").html("Buffer");
        }
    });

然后当我单击按钮时出现此错误

turf.min.js:1 Uncaught Error: options must be an object
    at Object.t.buffer (turf.min.js:1)
    at HTMLButtonElement.<anonymous> (js.js:112)
    at HTMLButtonElement.dispatch (jquery-3.3.1.slim.min.js:2)
    at HTMLButtonElement.v.handle (jquery-3.3.1.slim.min.js:2)
4

1 回答 1

2

正如错误所暗示的,您没有传递有效的“选项”参数(在这种情况下,您发送的是字符串“英里”而不是选项对象)。根据文档(http://turfjs.org/docs#buffer),您应该调用

var buff =turf.buffer(incidences2, 1, {units: 'miles'});

于 2018-10-03T12:22:47.320 回答