-
-
Save typelogic/b91b00bf3901d538bf0e0bc37993d713 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.
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
#!/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