2

Can anybody explain how to get geojson features .Can somebody show way to get attributes from geojson text.

OpenLayers.ProxyHost = "/gt/proxy.cgi?url=";
     var lon = 5;
    var lat = 44;
    var zoom = 5;
    var map, layer;

    //function init(){
       var bounds = new OpenLayers.Bounds(
                -125051.196, 3967670.8507,
                1549596.1896, 4677667.0145
            );
            var options = {
                controls: [],
                maxExtent: bounds,
                maxResolution: 6541.59135,
                projection: "EPSG:23036",
                units: 'm'
            };
    map = new OpenLayers.Map('map', options);
    map.addControl(new OpenLayers.Control.PanZoomBar({
                position: new OpenLayers.Pixel(2,10)
            }));

            map.addControl(new OpenLayers.Control.Navigation());
            //map.addControl(new OpenLayers.Control.Scale($('scale')));
            map.addControl(new OpenLayers.Control.MousePosition({element: $('location')}));
             var geojson_format = new OpenLayers.Format.GeoJSON();
        var vl2 = new OpenLayers.Layer.Vector("",
        {isBaseLayer: true,
         styleMap: new OpenLayers.StyleMap({'default':{
                strokeColor: "#00FF00",
                strokeOpacity: 1,
                strokeWidth: 1,
                fillColor: "#FF5500",
                fillOpacity: 0.5,
                label : "${ad}",
                fontSize: "8px",
                fontFamily: "Courier New, monospace", 
                labelXOffset: "0.5",
                labelYOffset: "0.5"
            }})
        });
        map.addLayer(vl2);

       function handler(request) {

        vl2.addFeatures(geojson_format.read(request.responseText))
        map.zoomToExtent(bounds);
        }
        var request = OpenLayers.Request.GET({
        url: "myurljson&srsName=EPSG:23036",
        params: {},
        callback: handler
    });

please show me some way

3
  • Which software? OpenLayers? be more specific.
    – Pablo
    Aug 15, 2012 at 19:05
  • For the attributes are you talking about the geojson properties value? Remember, geojson is just JSON that can be read into a javascript object using JSON.parse(json_str);
    – om_henners
    Aug 16, 2012 at 1:16
  • i create in openlayers pablo i use two layers base layers geojson the other one is gml i want to show when somebody click my city from map i want to show city name and the other attributes. There all features in geojson text i can see but i didnt use it .
    – rusty1119
    Aug 16, 2012 at 6:33

2 Answers 2

4

To create a vector layer for your map from a geojson string, the following should work (with changes depending on your environment). The documentation from the OpenLayers API on the GeoJSON read function would be worth taking a look at as well.

var in_options = {
                'internalProjection': map.projection,
                'externalProjection': map.projection
                 };
var geojson_format = new OpenLayers.Format.GeoJSON(in_options);
var vectors = new OpenLayers.Layer.Vector("Some name", {style_options_etc});

var features = geojson_format.read(geosjon_str, "FeatureCollection");

//vectors.addFeatures requires an array, thus
if(features.constructor != Array) {
    features = [features];
}

vectors.reportError = true;
vectors.addFeatures(features);
map.addLayers([features]);
2
  • Is there anybody answer my question plz ??
    – rusty1119
    Aug 17, 2012 at 13:29
  • I think the last line might be wrong, I could only get it working with map.addLayer(vectors); Mar 28, 2018 at 9:52
0

I hope this is what you're looking for. I was trying to extract attributes from geojson file using jquery. Posted this question and got my answer.

If you're not using jquery, just iterate with JavaScript

1
  • no geomajor im not using jquery .But maybe you have another way to this solution because i need it but thx again
    – rusty1119
    Aug 17, 2012 at 16:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.