Skip to content

Instantly share code, notes, and snippets.

@valeriyvan
Last active December 13, 2021 11:30
Show Gist options
  • Select an option

  • Save valeriyvan/75e4ef1787cd237fe3943d0e97b135df to your computer and use it in GitHub Desktop.

Select an option

Save valeriyvan/75e4ef1787cd237fe3943d0e97b135df to your computer and use it in GitHub Desktop.

Revisions

  1. valeriyvan revised this gist Oct 7, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions MinMaxCoordinates.swift
    Original file line number Diff line number Diff line change
    @@ -10,8 +10,8 @@ let coordinates = [(CLLocationCoordinate2D(latitude:-40.0, longitude:38.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
    (
    (c.latitude > r.0.0 ? r.0.0 : c.latitude, c.latitude < r.0.1 ? r.0.1 : c.latitude),
    (c.longitude > r.1.0 ? r.1.0 : c.longitude, c.longitude < r.1.1 ? r.1.1 : c.longitude)
    (min(c.latitude,r.0.0), max(c.latitude, r.0.1)),
    (min(c.longitude, r.1.0), max(c.longitude, r.1.1))
    )
    }

  2. valeriyvan created this gist Oct 7, 2016.
    21 changes: 21 additions & 0 deletions MinMaxCoordinates.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    // 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
    (
    (c.latitude > r.0.0 ? r.0.0 : c.latitude, c.latitude < r.0.1 ? r.0.1 : c.latitude),
    (c.longitude > r.1.0 ? r.1.0 : c.longitude, c.longitude < r.1.1 ? r.1.1 : c.longitude)
    )
    }

    minLatitude // -40
    maxLatitude // 44.2
    minLongitude // 3.2
    maxLongitude // 154.2