user4173776
user4173776

Reputation:

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]); 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>

Upvotes: 0

Views: 543

Answers (1)

Troido
Troido

Reputation: 78

You are trying to declare an element in an array.

Remove the var keyword from the line var markers[i] = ...

You already declared the array, you don't need to declare its elements anymore.

Upvotes: 2

Related Questions