Updating / refreshing a live geojson layer | JSON & JS Variable

Posted by Ozaki on Stack Overflow See other posts from Stack Overflow or by Ozaki
Published on 2010-05-04T07:11:57Z Indexed on 2010/05/05 3:18 UTC
Read the original article Hit count: 458

Filed under:
|
|
|
|

TLDR I am trying to get my geoJSON layer to update, currently it will 1. Create the vector mark, 2. Remove the vector mark, 3. Set the JS variable for lat and lon, 4. Unset the variable??? :S


Hey S O. I have a geojson layer set up as follows:

//GeoJSON Layer//
var layer1 = new OpenLayers.Layer.GML("My GeoJSON Layer", "coordinates", 
             {format: OpenLayers.Format.GeoJSON,
             styleMap: style_red}); 

My features are set up as follows:

 var latitude = 0.0; // as 0.0 it will draw the point in.
 var longitude = 0.0; // as 0.0 it will draw the point in.
 //var longitude = getlongitude; where getlongitude = JSON string of longitude.
 var point = new OpenLayers.Geometry.Point(longitude, latitude);
 pointFeature = new OpenLayers.Feature.Vector(point, null, style_red);        
 var style_red = OpenLayers.Util.extend({}, layer_style);
 style_red.strokeColor = "red";
 style_red.fillColor = "black";
 style_red.fillOpacity = 0.5;
 style_red.graphicName = "circle";
 style_red.pointRadius = 3.8;
 style_red.strokeWidth = 2;
 style_red.strokeLinecap = "butt";

and my layer updating function:

function UpdateLayer(){
    var p = new OpenLayers.Format.GeoJSON({
                 'internalProjection': new OpenLayers.Projection("EPSG:900913"),
                 'externalProjection': new OpenLayers.Projection("EPSG:4326")
            });

    var url = "coordinates";
    OpenLayers.loadURL(url, {}, null, function(r) {
        var f = p.read(r.responseText);
        map.layers[2].destroyFeatures();
        map.layers[2].addFeatures(pointFeature);
    });

    setTimeout("UpdateLayer()",1000)  
 }

Any idea what I am doing wrong or what I am missing?

Edit1 It now removes the feature (was map.layers[1]) previously... But will not add the new feature..

Edit2 I managed to get it to redraw a point but not with live data. It should draw the point at what (latitude) & (longitude) are equal to. I am trying to set latitude & longitude to some JSON string but every time straight after it sets the variable it changes it back to "undefined" as soon as it passes the line after var latitude? (using firebug & firequery to debug)

© Stack Overflow or respective owner

Related posts about geojson

Related posts about openlayers