2

I apologize if this is trivial or not possible, I am new to GIS. So I have a geoJSON file with thousands of members, each with a point coordinate. Is there a way for me to access the first coordinate (and the last, but I will deal with that later) of the geoJSON file?

Extraneous info: I am working on an html page using leaflet and have converted a CSV file into a geoJSON file, but I don't want the initial set view to be hard coded. I want it to be set, ideally to the center of the points, but being set to the first point will also work.

1

You can easily get the center point of the bounding box that surrounds the geoJSON layer:

var points = L.geoJson(json,{});
var center = points.getBounds().getCenter();

Once you have that, set the map view to be centered on that point:

// assumes you've already set up a map object with L.map({}); set zoom to some zoom level
map.setView(center,zoom);

as pointed out in the comment by @ghybs, you can also just fit the view to the bounds of the points, no need for a center point or zoom level:

map.fitBounds(points.getBounds());
  • 1
    And to view all of them: map.fitBounds(points.getBounds()) With this you do not need to choose a zoom level. – ghybs Mar 1 '16 at 19:35
  • good point - adding that to the answer – toms Mar 1 '16 at 21:21
  • Where you have var points = L.geoJson(json,{}); is json supposed to be my geoJSON file/data? – M.Lanus Mar 2 '16 at 2:54
  • that is correct. If you add a more complete code sample to your question, I can try to be more specific your example – toms Mar 2 '16 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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