0

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>
4
  • 3
    did you try adding the ; on the line above vectorSource.addFeature(markers[i]); ? Commented Oct 25, 2014 at 19:17
  • var markers[i] = new ol.Feature({/* Some stuff */});. Note the semi-colon Commented Oct 25, 2014 at 19:17
  • The error may be before this code with a not even { Commented Oct 25, 2014 at 19:25
  • @eduyayo i added the full code here .. Commented Oct 25, 2014 at 19:30

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.