Reputation:
Here is the json response
[{"lat": "36.15900011", "lon": "-115.17205183"}, {"lat": "36.15899561", "lon": "-115.17276155"}]
And below code given this error:
TypeError: item is undefined
...: new ol.geom.Point(ol.proj.transform([item.lon , item.lat], 'EPSG:4326', 'EPSG:...
below the code inside the <script>
$.ajax({
url:'parser', success:function(response){
$(document).ready(function(){
var jsonlen = response.length;
for (var i=0; i<=jsonlen; i++){
var item = response[i];
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([item.lon , item.lat], 'EPSG:4326', 'EPSG:3857')),
name:'Null Island',
rainfall:500
})
var vectorSource = new ol.source.Vector({
// empty vector
})
vectorSource.addFeature(iconFeature);
}
I don't know how to give json response continuously to iconFeature
Upvotes: 0
Views: 177
Reputation: 9224
First of all, unwrap your code from the handler, it makes no sense here. I've amended it in the code.
Second, your for loop will run even if the length is empty. That's why you're getting undefined.
You need to use just less than, not less than or equal.
Because, if the length is zero, and i
initiates at zero, then the conditional will be true and response[0] will be called, even though the response is an empty array. Returning undefined
Third (and I can't help you with this), you have to check the response to make sure the data is present. It doesn't seem like it is.
$.ajax({
url: 'parser',
success: function (response) {
var jsonlen = response.length - 1;
for (var i = 0; i < jsonlen; i++) {
var item = response[i];
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([item.lon, item.lat], 'EPSG:4326', 'EPSG:3857')),
name: 'Null Island',
rainfall: 500
});
var vectorSource = new ol.source.Vector({
// empty vector
})
vectorSource.addFeature(iconFeature);
}
}
});
Upvotes: 1