Created
April 23, 2017 14:54
-
-
Save jsbain/2a69f3dbedd78eb52988afeed00558c3 to your computer and use it in GitHub Desktop.
objc_location.py
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
| from objc_util import * | |
| from threading import Thread, Condition, Event, current_thread,enumerate | |
| m=ObjCClass('CLLocationManager').sharedManager() | |
| class Location(): | |
| def __init__(self,cllocation): | |
| self.altitude=cllocation.altitude() | |
| self.lat=cllocation.coordinate().a | |
| self.lon=cllocation.coordinate().b | |
| self.speed=cllocation.speed() | |
| self.horiz_accy=cllocation.horizontalAccuracy() | |
| self.vert_accy=cllocation.verticalAccuracy() | |
| self.course=cllocation.course() | |
| self.timestamp=cllocation.timestamp().timeIntervalSince1970() | |
| self.description=str(cllocation.description()) | |
| def __repr__(self): | |
| return self.description | |
| def locationManager_didUpdateLocations_(_self,_cmd,locationManager,locations): | |
| print(ObjCInstance(locations)[0]) | |
| #this is where you would do your "work" | |
| def locationManager_didFailWithError_(_self,_cmd,locationManager,err): | |
| print(ObjCInstance(err)) | |
| raise Exception('Location failed') | |
| #try to recover instead? | |
| def locationManagerDidPauseLocationUpdates_(_self,_cmd,locationManager): | |
| print('updates were paused! ') | |
| #create delegate | |
| MyLocationDelegate=create_objc_class('MyLocationDelegate', | |
| methods=[locationManager_didUpdateLocations_, locationManager_didFailWithError_, locationManagerDidPauseLocationUpdates_], | |
| protocols=['CLLocationDelegate']) | |
| d=MyLocationDelegate.new() | |
| m.delegate=d | |
| import ui,time | |
| def get_location(): | |
| m.e=Event() | |
| def wait_for_response(): | |
| print('waiting') | |
| m.e.wait(1.) | |
| t=Thread(target=wait_for_response) | |
| t.start() | |
| print('req') | |
| print('done req') | |
| t.join() | |
| return Location(m.location()) | |
| import ui | |
| b=ui.Button(title='push') | |
| b.frame=(0,0,222,222) | |
| def a(sender): | |
| m.requestLocation() | |
| b.action=a | |
| b.present('sheet') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment