javascript - openlayer vectorSource error with json data -
var vectorsource = new ol.source.vector({ projection: 'epsg:4326' }); var vectorlayer = new ol.layer.vector({ source: vectorsource }); var map = new ol.map({ target: 'mapdiv', renderer: 'canvas', layers: [new ol.layer.tile({source: new ol.source.osm()}),vectorlayer], view: new ol.view({ center: ol.proj.transform([2.1833, 41.3833], 'epsg:4326', 'epsg:3857'), zoom: 2 }) }); var sessiondata = {"4798":{"location":[{"lat":27.714622020721,"lng":85.31263589859}]},"5873":{"location":[{"lat":59.944732189178,"lng":17.821261882782}]}}; createmarkers(sessiondata);//this function has issues //this function has no issue function addfeatures() { var i, lat, lon, geom, feature, features = []; for(i=0; i< 10; i++) { lat = math.random() * 174 - 87; lon = math.random() * 360 - 180; geom = new ol.geom.point(ol.proj.transform([lon,lat], 'epsg:4326', 'epsg:3857')); feature = new ol.feature(geom); features.push(feature); } vectorsource.addfeatures(features); } //this function show error function createmarkers(sessdata) { var geom, feature, features = []; $.each(sessdata,function(key,val) { var locations = val.location; if(location.length == 0) { return true; } $.each(locations,function(index,value) { if(value.lng == 0 || value.lat == 0) { return; } geom = new ol.geom.point(ol.proj.transform([value.lng,value.lat], 'epsg:4326', 'epsg:3857')); feature = new ol.feature(geom); features.push(feature); }); }); vectorsource.addfeature(features); } addfeatures , createmarkers function has similar codes. addfeatures function has no issues when createmarkers function called, shows error "uncaught typeerror: a.addeventlistener not function" @ line "vectorsource.addfeature(features)"
fixed adding (s) @ end of addfeature. addfeatures instead of addfeature.
Comments
Post a Comment