Last active
December 13, 2021 11:30
-
-
Save valeriyvan/75e4ef1787cd237fe3943d0e97b135df to your computer and use it in GitHub Desktop.
Lookup for min/max latitude/longitude with one pass on array with the only reduce call
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
// Having array of coordinates you would like show on map you have calculate center and span for map. | |
// For that you need min/max latitude/longitude of coordinates. | |
let coordinates = [(CLLocationCoordinate2D(latitude:-40.0, longitude:38.2)), | |
(CLLocationCoordinate2D(latitude:44.2, longitude:17.833)), | |
(CLLocationCoordinate2D(latitude:5.22, longitude:154.2)), | |
(CLLocationCoordinate2D(latitude:33.2, longitude:9.2)), | |
(CLLocationCoordinate2D(latitude:4.2, longitude:3.2))] | |
// Lookup is done with one pass on array with only reduce | |
let ((minLatitude, maxLatitude), (minLongitude, maxLongitude)) = coordinates.reduce(((DBL_MAX, DBL_MIN),(DBL_MAX, DBL_MIN))) { r, c in | |
( | |
(min(c.latitude,r.0.0), max(c.latitude, r.0.1)), | |
(min(c.longitude, r.1.0), max(c.longitude, r.1.1)) | |
) | |
} | |
minLatitude // -40 | |
maxLatitude // 44.2 | |
minLongitude // 3.2 | |
maxLongitude // 154.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment