I have a "place" object from Google Maps which has a set of coordinates that represent a bounding box for a given location, say London. Each set of coordinates has a latitude and longitude.
I have written the below code to find the centerpoint, but I am not sure if it does actually produce the centerpoint. What if the polygon has 5 points instead of 4? Also, can this be done in a more efficient way, with less operations?
function average(array) {
// Add together and then divide by the length
return _.reduce(array, function (sum, num) {
return sum + num;
}, 0) / array.length;
}
// I have a two-dimensional array that I want to get the average of
var coords = [
[ -1.2, 5.1 ],
[ -1.3, 5.2 ],
[ -1.8, 5.9 ],
[ -1.9, 5.8 ]
]
// So I get the first column
var lats = coords.map(function (coord) {
return coord[0];
})
// Then the second
var longs = coords.map(function (coord) {
return coord[1];
})
// And average each column out
console.log([average(lats), average(longs)])