Created
May 28, 2019 15:35
-
-
Save AndrewWCarson/5d112dc7a0580c03f6a73c58ebbc9e34 to your computer and use it in GitHub Desktop.
Outputs the latitude and longitude if Python has access to macOS Location Services.
This file contains hidden or 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
#!/usr/bin/python | |
import CoreLocation | |
CLValidAuth = [CoreLocation.kCLAuthorizationStatusAuthorized] | |
def locationServicesEnabled(): | |
if CoreLocation.CLLocationManager.locationServicesEnabled(): | |
return True | |
else: | |
return False | |
def appLocationAuthorized(): | |
status = CoreLocation.CLLocationManager.authorizationStatus() | |
if status in CLValidAuth: | |
return True | |
else: | |
return False | |
def location(): | |
if not locationServicesEnabled(): | |
exit("Location Services Disabled") | |
elif not appLocationAuthorized(): | |
exit("No Location Authorization") | |
else: | |
locationMgr = CoreLocation.CLLocationManager.alloc().init() | |
locationMgr.startUpdatingLocation() | |
while locationMgr.location() == None: | |
continue | |
lat = locationMgr.location().coordinate().latitude | |
lon = locationMgr.location().coordinate().longitude | |
return lat, lon | |
if __name__ == "__main__": | |
print location() |
Obviously lines 30-32 are redundant, but I was using the shortened variable names to play around with formatting the return values in a way that was easy to copy-pasta into Google and get Maps to immediately recognize.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a rough attempt to get macOS location services data. Lots of little ways to improve on this...