Skip to content

Commit

Permalink
Merge branch 'Daij-Djan-master'
Browse files Browse the repository at this point in the history
* Daij-Djan-master:
  clean up output
  addded %address option and replaced deprecated code
  • Loading branch information
fulldecent committed May 25, 2016
2 parents 8951e19 + 5f20111 commit e3a2af8
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 45 deletions.
20 changes: 11 additions & 9 deletions CoreLocationCLI.xcodeproj/project.pbxproj
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
B34796D41CF3319600088BED /* CLPlacemark+CNPostalAddress.swift in Sources */ = {isa = PBXBuildFile; fileRef = B34796D31CF3319600088BED /* CLPlacemark+CNPostalAddress.swift */; };
D97137001C38867000BA5442 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = D97136FF1C38867000BA5442 /* main.swift */; };
D97137071C38869900BA5442 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D97137061C38869900BA5442 /* CoreLocation.framework */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand All @@ -24,17 +24,16 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
D97136FC1C38867000BA5442 /* CoreLocationCLI.temp_caseinsensitive_rename */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = CoreLocationCLI.temp_caseinsensitive_rename; sourceTree = BUILT_PRODUCTS_DIR; };
B34796D31CF3319600088BED /* CLPlacemark+CNPostalAddress.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "CLPlacemark+CNPostalAddress.swift"; path = "CoreLocationCLI/CLPlacemark+CNPostalAddress.swift"; sourceTree = "<group>"; };
D97136FC1C38867000BA5442 /* CoreLocationCLI */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = CoreLocationCLI; sourceTree = BUILT_PRODUCTS_DIR; };
D97136FF1C38867000BA5442 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = main.swift; path = corelocationcli/main.swift; sourceTree = "<group>"; };
D97137061C38869900BA5442 /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
D97136F91C38867000BA5442 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D97137071C38869900BA5442 /* CoreLocation.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -44,16 +43,16 @@
D97136F31C38866F00BA5442 = {
isa = PBXGroup;
children = (
B34796D31CF3319600088BED /* CLPlacemark+CNPostalAddress.swift */,
D97136FF1C38867000BA5442 /* main.swift */,
D97137061C38869900BA5442 /* CoreLocation.framework */,
D97136FD1C38867000BA5442 /* Products */,
);
sourceTree = "<group>";
};
D97136FD1C38867000BA5442 /* Products */ = {
isa = PBXGroup;
children = (
D97136FC1C38867000BA5442 /* CoreLocationCLI.temp_caseinsensitive_rename */,
D97136FC1C38867000BA5442 /* CoreLocationCLI */,
);
name = Products;
sourceTree = "<group>";
Expand All @@ -75,7 +74,7 @@
);
name = CoreLocationCLI;
productName = corelocationcli;
productReference = D97136FC1C38867000BA5442 /* CoreLocationCLI.temp_caseinsensitive_rename */;
productReference = D97136FC1C38867000BA5442 /* CoreLocationCLI */;
productType = "com.apple.product-type.tool";
};
/* End PBXNativeTarget section */
Expand Down Expand Up @@ -115,6 +114,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B34796D41CF3319600088BED /* CLPlacemark+CNPostalAddress.swift in Sources */,
D97137001C38867000BA5442 /* main.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -205,14 +205,16 @@
D97137041C38867000BA5442 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
PRODUCT_NAME = CoreLocationCLI.temp_caseinsensitive_rename;
CODE_SIGN_IDENTITY = "Mac Developer";
PRODUCT_NAME = CoreLocationCLI;
};
name = Debug;
};
D97137051C38867000BA5442 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
PRODUCT_NAME = CoreLocationCLI.temp_caseinsensitive_rename;
CODE_SIGN_IDENTITY = "Mac Developer";
PRODUCT_NAME = CoreLocationCLI;
};
name = Release;
};
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D97136FB1C38867000BA5442"
BuildableName = "CoreLocationCLI.temp_caseinsensitive_rename"
BlueprintName = "CoreLocationCLI"
ReferencedContainer = "container:CoreLocationCLI.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D97136FB1C38867000BA5442"
BuildableName = "CoreLocationCLI.temp_caseinsensitive_rename"
BlueprintName = "CoreLocationCLI"
ReferencedContainer = "container:CoreLocationCLI.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D97136FB1C38867000BA5442"
BuildableName = "CoreLocationCLI.temp_caseinsensitive_rename"
BlueprintName = "CoreLocationCLI"
ReferencedContainer = "container:CoreLocationCLI.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D97136FB1C38867000BA5442"
BuildableName = "CoreLocationCLI.temp_caseinsensitive_rename"
BlueprintName = "CoreLocationCLI"
ReferencedContainer = "container:CoreLocationCLI.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
26 changes: 26 additions & 0 deletions CoreLocationCLI/CLPlacemark+CNPostalAddress.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// CLPlacemark+CNPostalAddress.swift
//
// Created by Dominik Pich on 5/23/16.
// Copyright © 2016 William Entriken. All rights reserved.
//

import CoreLocation
import Contacts

extension CLPlacemark {
var postalAddress : CNPostalAddress? {
get {
guard let addressdictionary = self.addressDictionary else {
return nil
}
let address = CNMutablePostalAddress()
address.street = addressdictionary["Street"] as? String ?? ""
address.state = addressdictionary["State"] as? String ?? ""
address.city = addressdictionary["City"] as? String ?? ""
address.country = addressdictionary["Country"] as? String ?? ""
address.postalCode = addressdictionary["ZIP"] as? String ?? ""
return address
}
}
}
91 changes: 62 additions & 29 deletions CoreLocationCLI/main.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,72 @@

import Foundation
import CoreLocation
import Contacts

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

var once = false
var verbose = false
var format: String? = nil
var format = "%latitude %longitude"

func start() -> Void {
func start() {
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = 2.0
self.locationManager.delegate = self
if self.verbose {
print("authorizationStatus: \(CLLocationManager.authorizationStatus())")
print("locationServicesEnabled: \(CLLocationManager.locationServicesEnabled())")
print("deferredLocationUpdatesAvailable: \(CLLocationManager.deferredLocationUpdatesAvailable())")
print("significantLocationChangeMonitoringAvailable: \(CLLocationManager.significantLocationChangeMonitoringAvailable())")
print("headingAvailable: \(CLLocationManager.headingAvailable())")
print("regionMonitoringAvailable: \(CLLocationManager.regionMonitoringAvailable)")
print("regionMonitoringAvailable for CLRegion: \(CLLocationManager.isMonitoringAvailableForClass(CLRegion))")
}
self.locationManager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation)
{
if let format = self.format {
var output = format
output = output.stringByReplacingOccurrencesOfString("%latitude", withString: "\(newLocation.coordinate.latitude)")
output = output.stringByReplacingOccurrencesOfString("%longitude", withString: "\(newLocation.coordinate.longitude)")
output = output.stringByReplacingOccurrencesOfString("%altitude", withString: "\(newLocation.altitude)")
output = output.stringByReplacingOccurrencesOfString("%direction", withString: "\(newLocation.course)")
output = output.stringByReplacingOccurrencesOfString("%speed", withString: "\(Int(newLocation.speed))")
output = output.stringByReplacingOccurrencesOfString("%h_accuracy", withString: "\(Int(newLocation.horizontalAccuracy))")
output = output.stringByReplacingOccurrencesOfString("%v_accuracy", withString: "\(Int(newLocation.verticalAccuracy))")
output = output.stringByReplacingOccurrencesOfString("%time", withString: newLocation.timestamp.description)
print(output)
} else {
print("\(newLocation.description)")
func printFormattedLocation(location: CLLocation, address: String? = nil) {
var output = self.format
output = output.stringByReplacingOccurrencesOfString("%latitude", withString: String(format: "%+.6f", location.coordinate.latitude))
output = output.stringByReplacingOccurrencesOfString("%longitude", withString: String(format: "%+.6f", location.coordinate.longitude))
output = output.stringByReplacingOccurrencesOfString("%altitude", withString: "\(location.altitude)")
output = output.stringByReplacingOccurrencesOfString("%direction", withString: "\(location.course)")
output = output.stringByReplacingOccurrencesOfString("%speed", withString: "\(Int(location.speed))")
output = output.stringByReplacingOccurrencesOfString("%h_accuracy", withString: "\(Int(location.horizontalAccuracy))")
output = output.stringByReplacingOccurrencesOfString("%v_accuracy", withString: "\(Int(location.verticalAccuracy))")
output = output.stringByReplacingOccurrencesOfString("%time", withString: location.timestamp.description)
if let address = address {
output = output.stringByReplacingOccurrencesOfString("%address", withString: address)
}
print(output)
if self.once {
exit(0)
}
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
let location = locations.first as! CLLocation

if format.rangeOfString("%address") != nil {
self.locationManager.stopUpdatingLocation()
self.geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) in
if let postalAddress = placemarks?.first?.postalAddress {
let formattedAddress = CNPostalAddressFormatter.stringFromPostalAddress(postalAddress, style: CNPostalAddressFormatterStyle.MailingAddress)
self.printFormattedLocation(location, address: formattedAddress)
}
else {
self.printFormattedLocation(location, address: "?")
}
self.locationManager.startUpdatingLocation()
})
} else {
printFormattedLocation(location)
}
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("ERROR: \(error.localizedDescription)")
print("LOCATION MANAGER ERROR: \(error.localizedDescription)")
exit(1)
}
}
Expand All @@ -74,20 +97,30 @@ func help() {
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("")
}

// Show help if requested
if Process.arguments.count > 0 && Process.arguments[0] == "-h" {
help()
exit(0)
}

let args = NSUserDefaults.standardUserDefaults()
let delegate = Delegate()
delegate.format = args.stringForKey("format")
delegate.verbose = args.boolForKey("verbose")
delegate.once = args.boolForKey("once")
for (i, argument) in Process.arguments.enumerate() {
switch argument {
case "-h":
help()
exit(0)
case "-once":
delegate.once = true
case "-verbose":
delegate.verbose = true
case "-format":
if Process.arguments.count > i+1 {
delegate.format = Process.arguments[i+1]
}
default:
break
}
}
delegate.start()

autoreleasepool({
Expand Down
Empty file modified LICENSE
100644 → 100755
Empty file.
21 changes: 14 additions & 7 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ and run from Terminal.
%h_accuracy (meters)
%v_accuracy (meters)
%time

%address (revsere geocode location)

the format defaults to '%%latitude/%%longitude (%%address)
# Output example

./CoreLocationCLI
<+39.96034992, -75.18981059> +/- 154.00m (speed -1.00 mps / course -1.00) @ 2010-07-30 12:35:01 -0400
<+39.96036986, -75.18980353> +/- 157.00m (speed 0.00 mps / course -1.00) @ 2010-07-30 12:35:40 -0400
<+39.96036986, -75.18980353> +/- 157.00m (speed 0.00 mps / course -1.00) @ 2010-07-30 12:35:48 -0400^C
./CoreLocationCLI


50.9438299979853/6.94104380198676 (Kaiser-Wilhelm-Ring 21
Cologne North Rhine-Westphalia 50672
Germany)

./CoreLocationCLI -once yes -format '%latitude : %longitude'
39.96036986 : -75.18980353
./CoreLocationCLI -once yes -format '%latitude : %longitude'


50.9438299979853 6.94104380198676

# Building

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

0 comments on commit e3a2af8

Please sign in to comment.