I tested you code on my server and it works fine with my JSON file. So my suspicion is that it could be the format of your JSON file. Can you include it in your original question please?
OpenLayers.Format.GeoJSON
's read operation takes in a type which is the format type of the JSON file. But since we're using the OpenLayers.Protocol.HTTP
to call it, we don't have the option of supplying it, which means we need to provide the data in FeatureCollection format.
Try this format: (it's what i'm using and it's working)
{ "type": "FeatureCollection",
"features":
[
{"type":"Feature", "properties":{"species":"Cat"}, "geometry":{"type":"Polygon", "coordinates":[[[-86.484375, 33.3984375], [-86.484375, 23.5546875], [-86.484375, 12.3046875], [-78.046875, 7.3828125], [-69.609375, 6.6796875], [-64.6875, 8.7890625], [-60.46875, 16.5234375], [-60.46875, 22.1484375], [-60.46875, 28.4765625], [-60.46875, 36.2109375], [-62.578125, 46.7578125], [-69.609375, 31.2890625], [-72.421875, 34.1015625], [-73.125, 34.1015625], [-75.9375, 34.1015625], [-79.453125, 34.1015625], [-79.453125, 31.9921875], [-86.484375, 45.3515625], [-87.890625, 44.6484375], [-86.484375, 33.3984375]]]}}
,
{"type":"Feature", "properties":{"species":"Alligator"}, "geometry":{"type":"Polygon", "coordinates":[[[-15.46875, 35.5078125], [21.09375, 29.8828125], [59.0625, 34.1015625], [73.125, 34.1015625], [94.921875, 32.6953125], [109.6875, 24.2578125], [111.796875, 10.1953125], [101.25, -0.3515625], [79.453125, 1.0546875], [78.75, 7.3828125], [98.4375, 8.7890625], [101.25, 14.4140625], [87.890625, 17.2265625], [69.609375, 13.0078125], [75.234375, 12.3046875], [75.234375, 10.8984375], [68.90625, 10.1953125], [68.203125, 13.0078125], [49.21875, 12.3046875], [49.921875, 8.0859375], [43.59375, 7.3828125], [42.890625, 13.0078125], [29.53125, 14.4140625], [-9.84375, -8.0859375], [-14.0625, -6.6796875], [21.796875, 17.2265625], [-28.828125, 35.5078125], [-15.46875, 35.5078125]]]}}
,
{"type":"Feature", "properties":{"species":"Elephant"}, "geometry":{"type":"Polygon", "coordinates":[[[-137.109375, -12.3046875], [-144.140625, -1.7578125], [-162.421875, -2.4609375], [-168.75, -31.2890625], [-167.34375, -56.6015625], [-143.4375, -56.6015625], [-138.515625, -35.5078125], [-139.21875, -49.5703125], [-132.1875, -57.3046875], [-113.203125, -54.4921875], [-97.03125, -48.8671875], [-84.375, -38.3203125], [-80.859375, -21.4453125], [-80.859375, -10.1953125], [-86.484375, -10.1953125], [-89.296875, -22.1484375], [-97.734375, -32.6953125], [-116.015625, -39.7265625], [-121.640625, -35.5078125], [-134.296875, -17.2265625], [-132.890625, -11.6015625], [-137.109375, -12.3046875]]]}}
]
}
Best Answer
I have put a projection setting in your source definition and it seems to work:
This is the result: http://jsfiddle.net/zzahmbff/3/
Perhaps this resource can help you to see different ways to load vector data: http://acanimal.github.io/thebookofopenlayers3/chapter03_03_vector_source.html