| 1 |
''' |
| 2 |
iOS GPS |
| 3 |
----------- |
| 4 |
''' |
| 5 |
|
| 6 |
from pyobjus import autoclass, protocol |
| 7 |
from pyobjus.dylib_manager import load_framework |
| 8 |
from plyer.facades import GPS |
| 9 |
|
| 10 |
load_framework('/System/Library/Frameworks/CoreLocation.framework') |
| 11 |
CLLocationManager = autoclass('CLLocationManager') |
| 12 |
|
| 13 |
|
| 14 |
class IosGPS(GPS): |
| 15 |
def _configure(self): |
| 16 |
if not hasattr(self, '_location_manager'): |
| 17 |
self._location_manager = CLLocationManager.alloc().init() |
| 18 |
|
| 19 |
def _start(self, **kwargs): |
| 20 |
self._location_manager.delegate = self |
| 21 |
|
| 22 |
self._location_manager.requestWhenInUseAuthorization() |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
self._location_manager.startUpdatingLocation() |
| 28 |
|
| 29 |
def _stop(self): |
| 30 |
self._location_manager.stopUpdatingLocation() |
| 31 |
|
| 32 |
@protocol('CLLocationManagerDelegate') |
| 33 |
def locationManager_didChangeAuthorizationStatus_(self, manager, status): |
| 34 |
if self.on_status: |
| 35 |
s_status = '' |
| 36 |
provider_status = '' |
| 37 |
provider = 'standard-ios-provider' |
| 38 |
if status == 0: |
| 39 |
provider_status = 'provider-disabled' |
| 40 |
s_status = 'notDetermined' |
| 41 |
elif status == 1: |
| 42 |
provider_status = 'provider-enabled' |
| 43 |
s_status = 'restricted' |
| 44 |
elif status == 2: |
| 45 |
provider_status = 'provider-disabled' |
| 46 |
s_status = 'denied' |
| 47 |
elif status == 3: |
| 48 |
provider_status = 'provider-enabled' |
| 49 |
s_status = 'authorizedAlways' |
| 50 |
elif status == 4: |
| 51 |
provider_status = 'provider-enabled' |
| 52 |
s_status = 'authorizedWhenInUse' |
| 53 |
self.on_status(provider_status, '{}: {}'.format( |
| 54 |
provider, s_status)) |
| 55 |
|
| 56 |
@protocol('CLLocationManagerDelegate') |
| 57 |
def locationManager_didUpdateLocations_(self, manager, locations): |
| 58 |
location = manager.location |
| 59 |
|
| 60 |
description = location.description.UTF8String() |
| 61 |
split_description = description.split('<')[-1].split('>')[0].split(',') |
| 62 |
|
| 63 |
lat, lon = [float(coord) for coord in split_description] |
| 64 |
acc = float(description.split(' +/- ')[-1].split('m ')[0]) |
| 65 |
|
| 66 |
speed = location.speed |
| 67 |
altitude = location.altitude |
| 68 |
course = location.course |
| 69 |
|
| 70 |
self.on_location( |
| 71 |
lat=lat, |
| 72 |
lon=lon, |
| 73 |
speed=speed, |
| 74 |
bearing=course, |
| 75 |
altitude=altitude, |
| 76 |
accuracy=acc) |
| 77 |
|
| 78 |
|
| 79 |
def instance(): |
| 80 |
return IosGPS() |