# Fill array with coordinates within geographical bounds in Leaflet

I want to create an array and fill it with geographical coordinates I get from a bounds variable. Basically, a for-loop shall create coordinate pairs within the specified bounds with a fixed distance between each coordinate pair. See example code:

``````var arr = [],
bounds = [[ 59.37519, 18.04616],[ 59.3606, 18.08169]],

for (var latitude = bounds, longitude = bounds; latitude > bounds && longitude < bounds; latitude=latitude-0.003, longitude=longitude+0.003 ){
var lat = latitude,
lon = longitude;
arr.push(lat,lon);

console.log(lat);
}
``````

The current result looks like this: Q1: I would like to fill each row entirely until i reach the lower right corner.

Q2: It would be nice to fill the array in the format of the bounds variable as coordinate pairs. At the moment it looks like: [ 59.37519, 18.04616, 59.37489, 18.04646, 59.37459,...]

Use two nested loops:

``````for (var i = minLat; i <= maxLat; i+=0.03) {
for (var j = minLng; j <= maxLng; j+=0.03) {
arr.push([i,j]);
}
}
``````

Also,

with a fixed distance between each coordinate pair.

keep in mind that the earth is not a flat surface, so a difference of 0.03 degrees of longitude is not the same over different latitudes.

• Works perfectly, thanks. Do you have an efficient idea for the fixed distance, too? Recalculate to meters? Other projection? Just in case, because I think that the area is sufficiently small to neglect this issue. – Alexejevitsch May 10 '16 at 13:27
• Overlaying a square grid on the surface of a geoid is, unfortunately, a very difficult problem in geodesy. As long as you are aware there is an error, and the error is small enough for your application, you should be fine. – IvanSanchez May 10 '16 at 13:55
• Found this formula with global precision of +-1m and it seems to do it pretty well. – Alexejevitsch May 13 '16 at 8:01
• do you also know a way to connect the coordinate pairs by a polyline throughout the whole raster? The polyline should be like in agriculture, where tractor drivers harvest parallel lines. – Alexejevitsch Jun 2 '16 at 12:04