我看到,JSONP 正在浏览器的“网络监控”选项卡中加载,并且在 ajax 加载数据后,该函数正在从 JSONP 正确调用,但在加载数据后,它在 JSONP 函数调用上出现以下错误:
TypeError: undefined is not a function (evaluating 'layerSrc['lsBbox'].readFeatures(response)')loadFeaturesBboxblock 引用><!DOCTYPE html> <html lang="ru"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>The Book of OpenLayers3 - Code samples</title> <link rel="stylesheet" href="progr/nm/ol.css" type="text/css"> <script src="progr/nm/jquery-3.1.1.min.js" type="text/javascript"></script> <script src="progr/nm/ol.js" type="text/javascript"></script> <style type="text/css"> .map { width: calc(100% - 9px); height: 500px; box-shadow: 4px 4px 20px black; border: 3px solid blue; float: left; } .tree { width: auto; border: 3px solid red; background: yellow; float: left; } </style> </head> <body> <dev id='mapBbox' class='map col-sm-6'></dev> <script> var loadFeaturesBbox = function(response){layerSrc['lsBbox'].addFeatures(layerSrc['lsBbox'].readFeatures(response));}; var layerSrc = { lsBbox: new ol.source.Vector({ format: new ol.format.GeoJSON(), loader: function(extent, resolution, projection){ $.ajax({ url: 'http://demo.opengeo.org/geoserver/wfs?service=WFS&request=GetFeature&version=1.1.0&typename=osm%3Awater_areas&outputFormat=text%2Fjavascript&format_options=callback%3AloadFeaturesBbox&srsname=EPSG%3A3857&bbox='+extent.join(','), dataType: 'jsonp' }) }, strategy: ol.loadingstrategy.bbox, projection: 'EPSG:3857' }) }; var layers = { OSM: new ol.layer.Tile({source: new ol.source.OSM()}), lsBbox: new ol.layer.Vector({ source: layerSrc['lsBbox'], style: new ol.style.Style({ stroke: new ol.style.Stroke({ color: 'green', width: 2 }) }) }) }; var view = new ol.View({ center: ol.proj.transform([-75.923853, 45.428736], 'EPSG:4326', 'EPSG:3857'), maxZoom: 19, zoom: 10 }); var maps = { mapBbox: new ol.Map({ target: 'mapBbox', renderer: 'canvas', layers: [layers['OSM'], layers['lsBbox']], view: view }) }; </script> </body> </html>
因此,通过 JSONP 调用加载的矢量数据不会显示在 map 上。 我该如何解决这个问题?
OpenLayers版本:v3.20.1
最佳答案
矢量源上没有 readFeatures 方法,请参阅:http://openlayers.org/en/latest/apidoc/ol.source.Vector.html 您需要对其格式进行调用,因此 source.getFormat().readFeatures
关于javascript - openlayers3 loadingstrategy jsonp readFeatures 给出 'undefined is not a function',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42130066/