Skip to content

Instantly share code, notes, and snippets.

@cmittendorf
Created April 23, 2015 07:32
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save cmittendorf/2c6eefc6dd128b323428 to your computer and use it in GitHub Desktop.
Save cmittendorf/2c6eefc6dd128b323428 to your computer and use it in GitHub Desktop.
A command line tool for Mac OS X written in Swift that uses CoreLocation for reverse geocoding a location from latitude and longitude. The geocoding part of this script will work on OS X as well as iOS.
#!/usr/bin/env swift
// Run this command line tool as a dynamic script or compile a binary
// using the following command:
// swiftc -sdk `xcrun --show-sdk-path` LocateMe.swift
import Cocoa
import CoreLocation
extension String {
func doubleValue() -> Double {
return (self as NSString).doubleValue
}
}
var shouldKeepRunning = true
let runLoop = NSRunLoop.currentRunLoop()
let distantFuture = NSDate.distantFuture() as! NSDate
let args = Process.arguments
if args.count != 3 {
println("usage: \(args[0].lastPathComponent) <lat> <lon>")
exit(-1)
}
let lat: Double = Double(args[1].doubleValue())
let lon: Double = Double(args[2].doubleValue())
let location = CLLocation(latitude: lat, longitude: lon)
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) { locations, error in
if let error = error {
NSLog("\(error)")
} else {
if let placemarks = locations as? [CLPlacemark] {
for placemark in placemarks {
println("ISOcountryCode: \(placemark.ISOcountryCode)")
println("country: \(placemark.country)")
println("postalCode: \(placemark.postalCode)")
println("administrativeArea: \(placemark.administrativeArea)")
println("locality: \(placemark.locality)")
println("subLocality: \(placemark.subLocality)")
println("subThoroughfare: \(placemark.subThoroughfare)")
}
}
}
shouldKeepRunning = false
}
while shouldKeepRunning == true &&
runLoop.runMode(NSDefaultRunLoopMode, beforeDate: distantFuture) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment