[英]SyntaxError: missing ; before statement when inside for loop
Okay this question with title may already asked but i can't figure out why this not coming.. 好的,这个带有标题的问题可能已经问过了,但是我不知道为什么这个没来。
All i need is when for loop run i need to increment markers
also and added with vectorSource.addFeature(markers[i]); 我需要的是for循环运行时,我还需要增加
markers
并添加到vectorSource.addFeature(markers [i]);中。 but throwing error only .. why ? 但是只抛出错误..为什么?
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]);
}
throwing error like 抛出像这样的错误
SyntaxError: missing ; before statement
var markers[i] = new ol.Feature({
Updates: 更新:
sorry for posting the full code , i just need to clear this error.. 很抱歉张贴完整代码,我只需要清除此错误即可。
<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>
You are trying to declare an element in an array. 您正在尝试在数组中声明一个元素。
Remove the var
keyword from the line var markers[i] = ...
从行
var markers[i] = ...
删除var
关键字var markers[i] = ...
You already declared the array, you don't need to declare its elements anymore. 您已经声明了数组,不需要再声明其元素了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.