Skip to content

Instantly share code, notes, and snippets.

View AdrianBinDC's full-sized avatar

Adrian AdrianBinDC

  • DC Metro Area
View GitHub Profile
@AdrianBinDC
AdrianBinDC / gist:a8da4748181d86203689bc369fa01ed2
Last active July 5, 2018 14:09
Set MKMapRegion to display a specific zoom scale

Set Zoom Scale on Map Region in Swift

MKCoordinateRegionMakeWithDistance can be used to make a region with a specified distance (in meters).

Here is the method signature:

func MKCoordinateRegionMakeWithDistance(_ centerCoordinate: CLLocationCoordinate2D, 
                                        _ latitudinalMeters: CLLocationDistance, 
                                        _ longitudinalMeters: CLLocationDistance) -> MKCoordinateRegion

Use an extension to get your desired distance and convert it into meters.

@AdrianBinDC
AdrianBinDC / ConvertDistances.md
Last active December 15, 2023 02:12
Convert Distances in Swift using Measurement

Distance conversion in Swift

As of iOS 10, the Measurement class was introduced, which obviates the need lookup values and create constants to convert values. Here is a simple extension to convert distances.

extension Double {
  func convert(from originalUnit: UnitLength, to convertedUnit: UnitLength) -> Double {
    return Measurement(value: self, unit: originalUnit).converted(to: convertedUnit).value
  }
}
@AdrianBinDC
AdrianBinDC / Unofficial-APOD.md
Last active July 3, 2018 16:03
Unofficial APOD

Unofficial APOD

Unofficial APOD utilizes Alamofire to request data from NASA's APOD servers. The response data is returned in JSON format. Unofficial APOD utilizes structs conforming to Swift's Codable protocol to parse the JSON returns and persists the data in Core Data. Prior to requesting information from NASA's servers, Unofficial APOD first checks to see if it has already retrieved the data before making a network request.

NASA returns a JSON with text data about the image, but no binary data is returned. Unofficial APOD utilizes AlamofireImage to download the most recent 60 days' worth of non-copywritten images on a background thread and persists them locally to speed up performance for the user.

Copywritten images are not stored on the device. So the user doesn't see an empty screen, the UICircularProgressRing framework is configured to display the status of the image download.

Giv