Created
March 22, 2016 15:50
-
-
Save codenamev/14a2a0c5370f00ab1dae to your computer and use it in GitHub Desktop.
Calculate the largest latitude and longitude deltas needed to fit a set of points into based on an existing center point.
This file contains hidden or 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
/** | |
* Given a pre-defined set of coordinates as {latitude: ..., longitude: ...} | |
* and a pre-defined center point with {latitude: ..., longitude: ...} | |
* Find the zoom deltas needed for maps | |
** | |
// Example: | |
var center = {latitude: 40.0348483, longitude: -73.00318473}; | |
var points = [{latitude: ..., longitude: ...}, {latitude: ..., longitude: ...}, ...] | |
zoomDeltas(center, points); // => {latitudeDelta: ..., longitudeDelta: ...} | |
*/ | |
var centerPoint = { latitude: 40.699878, longitude: -75.207924 }; | |
var maxLatDelta = 0.0; | |
var maxLongDelta = 0.0; | |
var maxCoordinateDistance = function (point1, point2) { | |
// finding "furthest distance" means finding | |
// the maximum of the x/y deltas | |
var latDelta = Math.max(point1.latitude, point2.latitude) - Math.min(point1.latitude, point2.latitude); | |
var longDelta = Math.max(point1.longitude, point2.longitude) - Math.min(point1.longitude, point2.longitude); | |
return { | |
latitudeDelta: latDelta, | |
longitudeDelta: longDelta | |
} | |
} | |
var zoomDeltas = function(centerPoint, coordinates) { | |
for (var coordinate in coordinates) { | |
var distanceFromCenter = maxCoordinateDistance(centerPoint, coordinate); | |
maxLatDelta = Math.max(maxLatDelta, distanceFromCenter.latitudeDelta); | |
maxLongDelta = Math.max(maxLongDelta, distanceFromCenter.longitudeDelta); | |
} | |
// maxLatDelta and maxLongDelta are now the furthest radial distance from | |
// the center point, so multiply by 2 will give you the deltas you need | |
// for zooming a map | |
return { | |
latitudeDelta: maxLatDelta * 2, | |
longitudeDelta: maxLongDelta * 2 | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you sir just saved my life