Created
August 26, 2018 17:05
-
-
Save v1nn1k/a7b973b2e9425ec9c4a3546e6ee098fc to your computer and use it in GitHub Desktop.
Get map region for array of points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const getRegionForCoordinates = (points) => { | |
if (!points.length) { | |
return { | |
// By default show whole Prague | |
latitude: 50.0755, | |
longitude: 14.4378, | |
latitudeDelta: 0.1, | |
longitudeDelta: 0.1 | |
} | |
} | |
// points should be an array of { latitude: X, longitude: Y } | |
let minX | |
let maxX | |
let minY | |
let maxY | |
// init first point | |
;((point) => { | |
minX = point[0] | |
maxX = point[0] | |
minY = point[1] | |
maxY = point[1] | |
})(points[0]) | |
// calculate rect | |
points.forEach((point) => { | |
minX = Math.min(minX, point[0]) | |
maxX = Math.max(maxX, point[0]) | |
minY = Math.min(minY, point[1]) | |
maxY = Math.max(maxY, point[1]) | |
}) | |
const midX = (minX + maxX) / 2 | |
const midY = (minY + maxY) / 2 | |
let deltaX = maxX - minX | |
let deltaY = maxY - minY | |
// Reserve 10% square free | |
deltaY *= 1.1 | |
deltaX *= 1.1 | |
return { | |
latitude: midX, | |
longitude: midY, | |
latitudeDelta: deltaX, | |
longitudeDelta: deltaY | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment