Created
January 15, 2016 08:24
-
-
Save pudquick/329142c1740500bd3797 to your computer and use it in GitHub Desktop.
python/pyobjc method for accessing location on an OS X device
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
# Note: This _will_ GUI prompt a user for permission to locate them via python, even if run as root | |
from CoreLocation import CLLocationManager, kCLDistanceFilterNone, kCLLocationAccuracyThreeKilometers | |
from Foundation import NSRunLoop, NSDate, NSObject | |
is_enabled = CLLocationManager.locationServicesEnabled() | |
is_authorized = CLLocationManager.authorizationStatus() | |
class MyLocationManagerDelegate(NSObject): | |
def init(self): | |
self = super(MyLocationManagerDelegate, self).init() | |
if not self: | |
return | |
self.locationManager = CLLocationManager.alloc().init() | |
self.locationManager.setDelegate_(self) | |
self.locationManager.setDistanceFilter_(kCLDistanceFilterNone) | |
self.locationManager.setDesiredAccuracy_(kCLLocationAccuracyThreeKilometers) | |
self.locationManager.startUpdatingLocation() | |
return self | |
def locationManager_didUpdateToLocation_fromLocation_(self, manager, newloc, oldloc): | |
print "NEW:", newloc.description() | |
if oldloc is not None: | |
print "OLD:", oldloc.description() | |
else: | |
print "OLD: <None>" | |
def locationManager_didFailWithError_(self, manager, err): | |
print "ERR:", err.description() | |
def main(): | |
finder = MyLocationManagerDelegate.alloc().init() | |
for x in range(5): | |
print "loop", x | |
NSRunLoop.currentRunLoop().runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment