我正在尝试使用下面的python脚本来查找我的Mac的当前位置。它使用python目标-C桥,有时也能工作。然而,有时我会得到以下的AttributeError,我不确定我应该做什么来修复错误。
#!/usr/bin/python
# encoding: utf-8
import CoreLocation
manager = CoreLocation.CLLocationManager.alloc().init()
manager.delegate()
manager.startUpdatingLocation()
coord = manager.location().coordinate()
lat, lon = coord.latitude, coord.longitude
print lat, lon以下错误:
Traceback (most recent call last):
File "Desktop/SimpleCoreServices.py", line 11, in <module>
coord = manager.location().coordinate()
AttributeError: 'NoneType' object has no attribute 'coordinate'苹果的开发者文档没有帮助我,因为我的目标-C没有那么强大。
发布于 2018-09-19 20:16:16
您应该等到locationd提示位置访问。然后允许python使用位置服务。我在代码中添加了一个等待块:
import CoreLocation
from time import sleep
manager = CoreLocation.CLLocationManager.alloc().init()
manager.delegate()
manager.startUpdatingLocation()
while CoreLocation.CLLocationManager.authorizationStatus() != 3 or manager.location() is None:
sleep(.1)
coord = manager.location().coordinate()
lat, lon = coord.latitude, coord.longitude
print (lat, lon)在循环等待authorizationStatus并在location中获取值时,将显示提示符。有时,显示对话框需要大约30秒:

如果用户通过选择“允许”接受访问,则authorizationStatus的值将变为3。在此之后,循环将继续等待,直到获得一个位置值。
不幸的是,有时不可能通过系统首选项中的安全和隐私部分来控制访问。由于在Location列表中消失了python行,所以不能检查或取消选中。而且,tccutil shell命令无法控制这一点。
如果您不小心选择了“不允许”按钮,则可以使用这些指令重置该按钮。
https://stackoverflow.com/questions/29755998
复制相似问题