I'm using OL 3.6 with no additional plugins.

Is there a way to store points from a GeoJSON file to an array?

I'm thinking it would look something like this(?):

var features = new Array(of_some_size);
for(var i = 0; i < of_some_size; i++)
features[i] = new ol.format.GeoJSON(
    url: './testData.geojson')

The file is local and this is just for personal use and I want it to work offline first and foremost, and I would like to try and shy away from ajax requests just because I am very new to javascript and OpenLayers and I am definitely still trying to grasp everything I've learned so far. I don't want to exclude it as a solution but I would prefer not to, if that's the only way, then that's the only way, nothing you can do about it. The .geojson looks like this:


I edited the above geojson so it includes but does not go past the point that causes it to fail for me ([90.610383,104.58641]). If I change the 104 to anything below 90 it works. Now why this is happening I have absolutely no idea and I've double checked the formatting 5 times over. If you take out the last point it works.

This is a JSfiddle with what I'm trying to do for more clarifiaction. If you un-comment the first bit of code it will work fine, if you want to use the chunk I wrote everything loads except for the cluster layer. I feel like this should work but I'm not sure why it doesn't. Possibly an error reading in the geoJSON(I know it will not work in the fiddle because I don't have my .geojson hosted anywhere but it doesn't work loading directly to my browser either). I know it doesn't have the .geojson in the external files so I will add more points to the example above so you can maybe load it on your own machine?

1 Answer 1


It seems you re mixing some concepts

features[i] = new ol.format.GeoJSON(
    url: './testData.geojson')

The above code can't work because the ol.format.GeoJSON API does not accept url as a parameter eg the official doc. You also try to loop on a GeoJSON call (if I consider above sample was intented for this)

The normal way (there are more than one) could be the following:

  • make a call using Ajax without relying on the OpenLayers 3 library with something like below :

    var url = 'testData.geojson';
    var r = new XMLHttpRequest();
    r.open("GET", url, true);
    r.onreadystatechange = function () {
        if (r.readyState != 4 || r.status != 200) {
  • Then you need to parse the response (it's a string not a js object) with

    var obj = JSON.parse(r.responseText);
  • Finally, just loop, through the obj.features (it's a js array) to convert it to a simpler array using the following

    var features = obj.features;
    for (var i = 0, len = features.length; i < len; i++) {
        var feat = features[i].properties;
        feat['type'] = features[i].geometry.type;
        feat['coordinates'] = features[i].geometry.coordinates;

The last part IMO, should not be used. I answered this last part due to your question but if you don't use a FeatureCollection (GeoJSON is a bit more complex although quite simple), you will have to "reinvent the wheel" with boiler plate code. Moreover, the features within a GeoJSON are already a js array.

For going back and forth between string and json objects, take a look at JSON.stringify and JSON.parse. It can be useful depending of the storage choice for your offline use (if you choose HTML 5 storage).

  • Thanks a lot, very helpful! The reason I was asking was because I am attempting to replicate something like openlayers.org/en/v3.6.0/examples/cluster.html?mode=advanced but every time I try to add data like this: var originalSource = new ol.source.Vector({ url: 'test.geojson', format: new ol.format.GeoJSON({ defaultDataProjection :'EPSG:4326', projection: 'EPSG:3857' }) }); It doesn't work and they show adding points to a features array.
    – trevinator
    Jun 16, 2015 at 15:21
  • Take a look at this sample bl.ocks.org/ThomasG77/badfca2694a7255fa2fa It does what you expect. IMO, next time, you should better ask about the first issue before, instead of asking for code to bypass it.
    – ThomasG77
    Jun 16, 2015 at 15:55
  • this is exactly why I asked the qestion because I tried just that and it doesn't work so I looked for a way to bypass the problem or if anyone could identify why it wasn't working
    – trevinator
    Jun 16, 2015 at 16:08
  • Could it be the contents of my .geojson? Can clusters not handle specified properties? Even when I copy and paste your index.html into my file and load it, it doesn't work with my .geojson file. But i know my .geojson is formatted right because it works with other applications like just plotting the locations
    – trevinator
    Jun 16, 2015 at 16:17
  • could it also be the coordinate system I am using? I notice in your sample .geojson your coordinates are typically much higher numbers than mine are you using a different projection?
    – trevinator
    Jun 16, 2015 at 18:18

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.