Skip to content

Commit 2ae8ab4

Browse files
committed
support json format, fixes #13
1 parent 401de7e commit 2ae8ab4

File tree

2 files changed

+74
-62
lines changed

2 files changed

+74
-62
lines changed

CoreLocationCLI/main.swift

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import CoreLocation
1111
import Contacts
1212

1313
class Delegate: NSObject, CLLocationManagerDelegate {
14-
let locationManager = CLLocationManager()
1514
let geoCoder = CLGeocoder()
16-
15+
let locationManager = CLLocationManager()
1716
var once = false
1817
var verbose = false
1918
var format = "%latitude %longitude"
@@ -44,8 +43,8 @@ class Delegate: NSObject, CLLocationManagerDelegate {
4443

4544
func printFormattedLocation(_ location: CLLocation, address: String? = nil) {
4645
var output = self.format
47-
output = output.replacingOccurrences(of: "%latitude", with: String(format: "%+.6f", location.coordinate.latitude))
48-
output = output.replacingOccurrences(of: "%longitude", with: String(format: "%+.6f", location.coordinate.longitude))
46+
output = output.replacingOccurrences(of: "%latitude", with: String(format: "%0.6f", location.coordinate.latitude))
47+
output = output.replacingOccurrences(of: "%longitude", with: String(format: "%0.6f", location.coordinate.longitude))
4948
output = output.replacingOccurrences(of: "%altitude", with: "\(location.altitude)")
5049
output = output.replacingOccurrences(of: "%direction", with: "\(location.course)")
5150
output = output.replacingOccurrences(of: "%speed", with: "\(Int(location.speed))")
@@ -55,14 +54,17 @@ class Delegate: NSObject, CLLocationManagerDelegate {
5554
if let address = address {
5655
output = output.replacingOccurrences(of: "%address", with: address)
5756
}
58-
print("Location: \(output)")
57+
print(output)
5958
if self.once {
6059
exit(0)
6160
}
6261

6362
}
6463

6564
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
65+
guard verbose else {
66+
return
67+
}
6668
switch status {
6769
case .authorizedAlways:
6870
print("Location access authorized.")
@@ -104,39 +106,40 @@ class Delegate: NSObject, CLLocationManagerDelegate {
104106
print("LOCATION MANAGER ERROR: \(error.localizedDescription)")
105107
exit(1)
106108
}
107-
}
108-
109109

110-
func help() {
111-
print("USAGE: CoreLocationCLI [options]")
112-
print(" Displays current location using CoreLocation services.")
113-
print(" By default, this will continue printing locations until you kill it with Ctrl-C.")
114-
print("")
115-
print("OPTIONS:")
116-
print(" -h Display this help message and exit")
117-
print("")
118-
print(" -once YES Print one location and exit")
119-
print(" -verbose YES Verbose mode")
120-
print(" -format 'format' Print a formatted string with the following specifiers")
121-
print(" %%latitude")
122-
print(" %%longitude")
123-
print(" %%altitude (meters)")
124-
print(" %%direction (degrees from true north)")
125-
print(" %%speed (meters per second)")
126-
print(" %%h_accuracy (meters)")
127-
print(" %%v_accuracy (meters)")
128-
print(" %%time")
129-
print(" %%address (revsere geocode location)")
130-
print("")
131-
print(" the format defaults to '%%latitude/%%longitude (%%address)")
132-
print("")
110+
func help() {
111+
print("USAGE: CoreLocationCLI [options]")
112+
print(" Displays current location using CoreLocation services.")
113+
print(" By default, this will continue printing locations until you kill it with Ctrl-C.")
114+
print("")
115+
print("OPTIONS:")
116+
print(" -h Display this help message and exit")
117+
print("")
118+
print(" -once YES Print one location and exit")
119+
print(" -verbose YES Verbose mode")
120+
print(" -format 'format' Print a formatted string with the following specifiers")
121+
print(" %%latitude Latitude (degrees north; or negative for south")
122+
print(" %%longitude Longitude (degrees west; or negative for east")
123+
print(" %%altitude Altitude (meters)")
124+
print(" %%direction Degrees from true north")
125+
print(" %%speed Meters per second")
126+
print(" %%h_accuracy Horizontal accuracy (meters)")
127+
print(" %%v_accuracy Vertical accuracy (meters)")
128+
print(" %%time Time")
129+
print(" %%address Reverse geocoded location to an address")
130+
print(" -json Use the format {\"latitude\":%latitude, \"longitude\":%longitude}")
131+
print(" Also implies -once")
132+
print("")
133+
print(" Default format if unspecified is: %%latitude %%longitude")
134+
print("")
135+
}
133136
}
134137

135138
let delegate = Delegate()
136139
for (i, argument) in ProcessInfo().arguments.enumerated() {
137140
switch argument {
138141
case "-h":
139-
help()
142+
delegate.help()
140143
exit(0)
141144
case "-once":
142145
delegate.once = true
@@ -146,6 +149,9 @@ for (i, argument) in ProcessInfo().arguments.enumerated() {
146149
if ProcessInfo().arguments.count > i+1 {
147150
delegate.format = ProcessInfo().arguments[i+1]
148151
}
152+
case "-json":
153+
delegate.once = true
154+
delegate.format = "{\"latitude\":%latitude, \"longitude\":%longitude}"
149155
default:
150156
break
151157
}

README.md

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,59 @@ is used, will exit after first location update. Otherwise, will
1111
continuously print output. After you download, be sure to chmod 755
1212
and run from Terminal.
1313

14-
USAGE: CoreLocationCLI [options]
15-
Displays current location using CoreLocation services.
16-
By default, this will continue printing locations until you kill it with Ctrl-C.
17-
18-
OPTIONS:
19-
-h Display this help message and exit
20-
21-
-once YES Print one location and exit
22-
-verbose YES Verbose mode
23-
-format 'format' Print a formatted string with the following specifiers
24-
%latitude
25-
%longitude
26-
%altitude (meters)
27-
%direction (degrees from true north)
28-
%speed (meters per second)
29-
%h_accuracy (meters)
30-
%v_accuracy (meters)
31-
%time
32-
%address (revsere geocode location)
33-
34-
the format defaults to '%%latitude/%%longitude (%%address)
35-
14+
```
15+
USAGE: CoreLocationCLI [options]
16+
Displays current location using CoreLocation services.
17+
By default, this will continue printing locations until you kill it with Ctrl-C.
18+
19+
OPTIONS:
20+
-h Display this help message and exit
21+
22+
-once YES Print one location and exit
23+
-verbose YES Verbose mode
24+
-format 'format' Print a formatted string with the following specifiers
25+
%%latitude Latitude (degrees north; or negative for south
26+
%%longitude Longitude (degrees west; or negative for east
27+
%%altitude Altitude (meters)
28+
%%direction Degrees from true north
29+
%%speed Meters per second
30+
%%h_accuracy Horizontal accuracy (meters)
31+
%%v_accuracy Vertical accuracy (meters)
32+
%%time Time
33+
%%address Reverse geocoded location to an address
34+
-json Use the format {\"latitude\":%latitude, \"longitude\":%longitude}
35+
Also implies -once
36+
37+
Default format if unspecified is: %%latitude %%longitude
38+
```
39+
3640
# Output example
3741

3842
./CoreLocationCLI
3943

44+
50.943829 6.941043
45+
46+
./CoreLocationCLI -once yes -format "%latitude %longitude\n%address"
4047

41-
50.9438299979853/6.94104380198676 (Kaiser-Wilhelm-Ring 21
48+
50.943829 6.941043
49+
Kaiser-Wilhelm-Ring 21
4250
Cologne North Rhine-Westphalia 50672
43-
Germany)
44-
45-
./CoreLocationCLI -once yes -format '%latitude : %longitude'
51+
Germany
52+
53+
./CoreLocationCLI -json
4654

55+
{"latitude":40.124159, "longitude":-75.036274}
4756

48-
50.9438299979853 6.94104380198676
49-
5057
# Building
5158

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

5461
xcodebuild
55-
56-
And then your executable can be run from this location:
62+
63+
And then your executable can be run from this location:
5764

5865
build/Release/CoreLocationCLI
5966

6067
# Contact
6168

6269
63-

0 commit comments

Comments
 (0)