Skip to content

Instantly share code, notes, and snippets.

@AndrewWCarson
Created May 28, 2019 15:35
Show Gist options
  • Save AndrewWCarson/5d112dc7a0580c03f6a73c58ebbc9e34 to your computer and use it in GitHub Desktop.
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.
#!/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()
@AndrewWCarson
Copy link
Author

This is a rough attempt to get macOS location services data. Lots of little ways to improve on this...

  • add a timeout for getting the location.
  • maybe change the CLValidAuth which was originally designed to handle other auth codes (turns out that macOS only has one of the codes)

@AndrewWCarson
Copy link
Author

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