Skip to content

Commit

Permalink
support json format, fixes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
fulldecent committed Oct 11, 2016
1 parent 401de7e commit 2ae8ab4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 62 deletions.
68 changes: 37 additions & 31 deletions CoreLocationCLI/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import CoreLocation
import Contacts

class Delegate: NSObject, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
let geoCoder = CLGeocoder()

let locationManager = CLLocationManager()
var once = false
var verbose = false
var format = "%latitude %longitude"
Expand Down Expand Up @@ -44,8 +43,8 @@ class Delegate: NSObject, CLLocationManagerDelegate {

func printFormattedLocation(_ location: CLLocation, address: String? = nil) {
var output = self.format
output = output.replacingOccurrences(of: "%latitude", with: String(format: "%+.6f", location.coordinate.latitude))
output = output.replacingOccurrences(of: "%longitude", with: String(format: "%+.6f", location.coordinate.longitude))
output = output.replacingOccurrences(of: "%latitude", with: String(format: "%0.6f", location.coordinate.latitude))
output = output.replacingOccurrences(of: "%longitude", with: String(format: "%0.6f", location.coordinate.longitude))
output = output.replacingOccurrences(of: "%altitude", with: "\(location.altitude)")
output = output.replacingOccurrences(of: "%direction", with: "\(location.course)")
output = output.replacingOccurrences(of: "%speed", with: "\(Int(location.speed))")
Expand All @@ -55,14 +54,17 @@ class Delegate: NSObject, CLLocationManagerDelegate {
if let address = address {
output = output.replacingOccurrences(of: "%address", with: address)
}
print("Location: \(output)")
print(output)
if self.once {
exit(0)
}

}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
guard verbose else {
return
}
switch status {
case .authorizedAlways:
print("Location access authorized.")
Expand Down Expand Up @@ -104,39 +106,40 @@ class Delegate: NSObject, CLLocationManagerDelegate {
print("LOCATION MANAGER ERROR: \(error.localizedDescription)")
exit(1)
}
}


func help() {
print("USAGE: CoreLocationCLI [options]")
print(" Displays current location using CoreLocation services.")
print(" By default, this will continue printing locations until you kill it with Ctrl-C.")
print("")
print("OPTIONS:")
print(" -h Display this help message and exit")
print("")
print(" -once YES Print one location and exit")
print(" -verbose YES Verbose mode")
print(" -format 'format' Print a formatted string with the following specifiers")
print(" %%latitude")
print(" %%longitude")
print(" %%altitude (meters)")
print(" %%direction (degrees from true north)")
print(" %%speed (meters per second)")
print(" %%h_accuracy (meters)")
print(" %%v_accuracy (meters)")
print(" %%time")
print(" %%address (revsere geocode location)")
print("")
print(" the format defaults to '%%latitude/%%longitude (%%address)")
print("")
func help() {
print("USAGE: CoreLocationCLI [options]")
print(" Displays current location using CoreLocation services.")
print(" By default, this will continue printing locations until you kill it with Ctrl-C.")
print("")
print("OPTIONS:")
print(" -h Display this help message and exit")
print("")
print(" -once YES Print one location and exit")
print(" -verbose YES Verbose mode")
print(" -format 'format' Print a formatted string with the following specifiers")
print(" %%latitude Latitude (degrees north; or negative for south")
print(" %%longitude Longitude (degrees west; or negative for east")
print(" %%altitude Altitude (meters)")
print(" %%direction Degrees from true north")
print(" %%speed Meters per second")
print(" %%h_accuracy Horizontal accuracy (meters)")
print(" %%v_accuracy Vertical accuracy (meters)")
print(" %%time Time")
print(" %%address Reverse geocoded location to an address")
print(" -json Use the format {\"latitude\":%latitude, \"longitude\":%longitude}")
print(" Also implies -once")
print("")
print(" Default format if unspecified is: %%latitude %%longitude")
print("")
}
}

let delegate = Delegate()
for (i, argument) in ProcessInfo().arguments.enumerated() {
switch argument {
case "-h":
help()
delegate.help()
exit(0)
case "-once":
delegate.once = true
Expand All @@ -146,6 +149,9 @@ for (i, argument) in ProcessInfo().arguments.enumerated() {
if ProcessInfo().arguments.count > i+1 {
delegate.format = ProcessInfo().arguments[i+1]
}
case "-json":
delegate.once = true
delegate.format = "{\"latitude\":%latitude, \"longitude\":%longitude}"
default:
break
}
Expand Down
68 changes: 37 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,59 @@ is used, will exit after first location update. Otherwise, will
continuously print output. After you download, be sure to chmod 755
and run from Terminal.

USAGE: CoreLocationCLI [options]
Displays current location using CoreLocation services.
By default, this will continue printing locations until you kill it with Ctrl-C.

OPTIONS:
-h Display this help message and exit

-once YES Print one location and exit
-verbose YES Verbose mode
-format 'format' Print a formatted string with the following specifiers
%latitude
%longitude
%altitude (meters)
%direction (degrees from true north)
%speed (meters per second)
%h_accuracy (meters)
%v_accuracy (meters)
%time
%address (revsere geocode location)

the format defaults to '%%latitude/%%longitude (%%address)
```
USAGE: CoreLocationCLI [options]
Displays current location using CoreLocation services.
By default, this will continue printing locations until you kill it with Ctrl-C.
OPTIONS:
-h Display this help message and exit
-once YES Print one location and exit
-verbose YES Verbose mode
-format 'format' Print a formatted string with the following specifiers
%%latitude Latitude (degrees north; or negative for south
%%longitude Longitude (degrees west; or negative for east
%%altitude Altitude (meters)
%%direction Degrees from true north
%%speed Meters per second
%%h_accuracy Horizontal accuracy (meters)
%%v_accuracy Vertical accuracy (meters)
%%time Time
%%address Reverse geocoded location to an address
-json Use the format {\"latitude\":%latitude, \"longitude\":%longitude}
Also implies -once
Default format if unspecified is: %%latitude %%longitude
```

# Output example

./CoreLocationCLI

50.943829 6.941043

./CoreLocationCLI -once yes -format "%latitude %longitude\n%address"

50.9438299979853/6.94104380198676 (Kaiser-Wilhelm-Ring 21
50.943829 6.941043
Kaiser-Wilhelm-Ring 21
Cologne North Rhine-Westphalia 50672
Germany)
./CoreLocationCLI -once yes -format '%latitude : %longitude'
Germany

./CoreLocationCLI -json

{"latitude":40.124159, "longitude":-75.036274}

50.9438299979853 6.94104380198676

# Building

To build this from the command line, run the compiler:

xcodebuild
And then your executable can be run from this location:

And then your executable can be run from this location:

build/Release/CoreLocationCLI

# Contact

Contact corelocationcli@phor.net

0 comments on commit 2ae8ab4

Please sign in to comment.