好的,这个带标题的问题可能已经问到了,但我无法弄清楚为什么这不会出现......
我需要的是,当循环运行时我还需要增加markers
并添加了vectorSource.addFeature(markers [i]);但只是抛出错误..为什么?
var markers = [];
for (var i = 0; i < jsonlen; i++) {
var item = response[i];
var markers[i] = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([item.lon, item.lat], 'EPSG:4326', 'EPSG:3857')),
name:'Null Island',
population: 4000,
})
vectorSource.addFeature(markers[i]);
}
像
一样抛出错误SyntaxError: missing ; before statement
var markers[i] = new ol.Feature({
更新
抱歉发布完整代码,我只需要清除此错误..
<script type="text/javascript">
$.ajax({
url:'parser', success:function(response){
$(document).ready(function(){
var jsonlen = response.length - 1;
var vectorSource = new ol.source.Vector({
// empty vector
})
var markers = [];
for (var i = 0; i < jsonlen; i++) {
var item = response[i];
var markers[i] = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([item.lon, item.lat], 'EPSG:4326', 'EPSG:3857')),
name:'Null Island',
population: 4000,
rainfall:500
});
vectorSource.addFeature(markers[i]);
}
//console.debug(response)
// icon feature started
//create the style
var iconStyle = new ol.style.Style({
image: new ol.style.Icon(/**@type {olx.style.IconOptions}*/({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
opacity: 0.75,
src: 'http://ol3js.org/en/master/examples/data/icon.png'
}))
});
//add the feature vector to the layer vector, and apply a style to whole layer
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: iconStyle
});
var map = new ol.Map({
layers: [new ol.layer.Tile({ source: new ol.source.OSM() }), vectorLayer],
target: document.getElementById('map'),
view: new ol.View({
center: [0, 0],
zoom: 3
})
});
})
}
})
</script>
答案 0 :(得分:2)
您正在尝试在数组中声明一个元素。
从第var
行
var markers[i] = ...
关键字
您已经声明了数组,您不再需要声明其元素。