openlayers3 loadingstrategy jsonp readFeatures给出'undefined不是函数'

时间:2017-02-09 06:49:42

标签: javascript jquery ajax jsonp openlayers-3

我看到,JSONP正在浏览器的网络监控选项卡中加载,并且在从JSONP正确调用ajax加载数据之后,该函数在加载数据后在JSONP函数调用上给出了以下错误:

TypeError:undefined不是函数(评估'layerSrc ['lsBbox']。readFeatures(response)')
loadFeaturesBbox

<!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调用加载的矢量数据不会显示在地图上。 我该如何解决这个问题呢? OpenLayers版本:v3.20.1

1 个答案:

答案 0 :(得分:2)

矢量源上没有readFeatures方法,请参阅:http://openlayers.org/en/latest/apidoc/ol.source.Vector.html 你需要在其格式上调用它,所以source.getFormat()。readFeatures