在OS X上使用python中的CoreLocation

2024-10-01 00:17:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用Python的corelocationapi获取Mac的当前位置。我知道我可以让他们调用一些命令行可执行文件或类似的东西,但我对使用CoreLocation自己的Python绑定感兴趣。在

到目前为止,我可以创建一个类作为委托,并创建CLLocationManager的实例。调用startUpdatingLocation()显示了OSX的位置权限请求程序,位置图标出现在菜单栏上,但我从未调用过任何委托方法。authorizationStatus()返回3,即kCLAuthorizationStatusAuthorized。在

怎么了?在

"""
Core Location Python test
"""

import Cocoa
import CoreLocation

locaitonSearchFinished = False

class CLTestDelegate(Cocoa.NSObject):
    def locationManager_didUpdateLocations_(self, manager, locations):
        print u"New Location: %s" % locations
        locaitonSearchFinished = True

    def locationManager_didFailWithError_(self, manager, error):
        print u"Error updating location: %s" % error
        locaitonSearchFinished = True

    def locationManager_didChangeAuthorizationStatus_(self, manager, status):
        print u"Status: %s" % status

delegate = CLTestDelegate.alloc().init()
locationManager = CoreLocation.CLLocationManager.alloc().init()
locationManager.setDelegate_(delegate)
locationManager.startUpdatingLocation()

print locationManager
print delegate
print CoreLocation.CLLocationManager.authorizationStatus()

while locaitonSearchFinished == False:
    pass

Tags: importselfdefmanagerlocationprintcorelocation委托