OpenLayers – Adding Multiple Vector Layer Markers with Data from JSON

jsonopenlayers

I would like to parse a local JSON file and add markers to a map based on the latitude and longitude parsed from the JSON file.

Here is a sample from the local JSON file:

"LOCATION_A": {
              "latitude": 10.2070,
              "longitude": 25.3215,
              "altitude": 1.2,
              "status": "CLOSED",
              "serverAddress": "192.168.0.1"
 }

I am unsure how to proceed beyond the following block of code:

var markers = new OpenLayers.Layer.Vector("Sites", {
  projection: map.displayProjection,
  strategies: [new OpenLayers.Strategy.Fixed()],
  protocol: new OpenLayers.Protocol.HTTP({
    url: "filename.json",
    format: new OpenLayers.Format.JSON()
    }),
  });

I am unsure how to use the parser to do this. I would like to use jQuery for this exercise now.

Best Answer

var json_array = <YOUR FEATURES>;

for (var item in json_array) {
  for(var value in item) {
    var feature = new Openlayers.Feature.Vector(
       new Openlayers.Geometry.Point(value.longitude, value.latitude),
       value
    );
    markers.addFeatures[feature]);
 }
}