草皮缓冲区传单geojson ajax

时间:2018-10-03 12:13:47

标签: javascript leaflet geojson turfjs

我已使用

从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)

1 个答案:

答案 0 :(得分:1)

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

<servlet> <servlet-name>MyJSP</servlet-name> <jsp-file>/WEB-INF/MyFolder/*</jsp-file> </servlet> <servlet-mapping> <servlet-name>MyJSP</servlet-name> <url-pattern>/ViewMyJsp.jsp</url-pattern> </servlet-mapping>