@@ -11,9 +11,8 @@ import CoreLocation
11
11
import Contacts
12
12
13
13
class Delegate : NSObject , CLLocationManagerDelegate {
14
- let locationManager = CLLocationManager ( )
15
14
let geoCoder = CLGeocoder ( )
16
-
15
+ let locationManager = CLLocationManager ( )
17
16
var once = false
18
17
var verbose = false
19
18
var format = " %latitude %longitude "
@@ -44,8 +43,8 @@ class Delegate: NSObject, CLLocationManagerDelegate {
44
43
45
44
func printFormattedLocation( _ location: CLLocation , address: String ? = nil ) {
46
45
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) )
49
48
output = output. replacingOccurrences ( of: " %altitude " , with: " \( location. altitude) " )
50
49
output = output. replacingOccurrences ( of: " %direction " , with: " \( location. course) " )
51
50
output = output. replacingOccurrences ( of: " %speed " , with: " \( Int ( location. speed) ) " )
@@ -55,14 +54,17 @@ class Delegate: NSObject, CLLocationManagerDelegate {
55
54
if let address = address {
56
55
output = output. replacingOccurrences ( of: " %address " , with: address)
57
56
}
58
- print ( " Location: \( output) " )
57
+ print ( output)
59
58
if self . once {
60
59
exit ( 0 )
61
60
}
62
61
63
62
}
64
63
65
64
func locationManager( _ manager: CLLocationManager , didChangeAuthorization status: CLAuthorizationStatus ) {
65
+ guard verbose else {
66
+ return
67
+ }
66
68
switch status {
67
69
case . authorizedAlways:
68
70
print ( " Location access authorized. " )
@@ -104,39 +106,40 @@ class Delegate: NSObject, CLLocationManagerDelegate {
104
106
print ( " LOCATION MANAGER ERROR: \( error. localizedDescription) " )
105
107
exit ( 1 )
106
108
}
107
- }
108
-
109
109
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
+ }
133
136
}
134
137
135
138
let delegate = Delegate ( )
136
139
for (i, argument) in ProcessInfo ( ) . arguments. enumerated ( ) {
137
140
switch argument {
138
141
case " -h " :
139
- help ( )
142
+ delegate . help ( )
140
143
exit ( 0 )
141
144
case " -once " :
142
145
delegate. once = true
@@ -146,6 +149,9 @@ for (i, argument) in ProcessInfo().arguments.enumerated() {
146
149
if ProcessInfo ( ) . arguments. count > i+ 1 {
147
150
delegate. format = ProcessInfo ( ) . arguments [ i+ 1 ]
148
151
}
152
+ case " -json " :
153
+ delegate. once = true
154
+ delegate. format = " { \" latitude \" :%latitude, \" longitude \" :%longitude} "
149
155
default :
150
156
break
151
157
}
0 commit comments