CoreLocation AttributeError [英] CoreLocation AttributeError
问题描述
我正在尝试使用以下python脚本查找Mac的当前位置。它正在使用python Objective-C桥,并且有时可以工作。但是有时我会收到以下AttributeError,但不确定如何解决该错误。
#!/ usr / bin / python
#编码: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
以下错误:
回溯(最近一次通话结束):
文件 Desktop / SimpleCoreServices.py,第11行,在< module>中
coord = manager.location()。coordinate()
AttributeError:'NoneType'对象没有属性'coordinate'
如果用户通过选择允许接受访问,则 authorizationStatus
的值将变为3。此后,循环将继续等待,直到获得位置值。
不幸的是,有时无法通过系统偏好设置中的安全性和隐私部分控制访问。由于定位服务列表中的python行消失了,因此无法选中或取消选中它。此外, tccutil
shell命令也无法控制此操作。
如果您不小心选择了不允许按钮,可以使用这些说明进行重置。
I am trying to find my Mac's current location using the following python script. It is using the python objective-C bridge and it works sometimes. However sometimes I am getting the following AttributeError and I'm unsure what I should do to fix the error.
#!/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
Following error:
Traceback (most recent call last):
File "Desktop/SimpleCoreServices.py", line 11, in <module>
coord = manager.location().coordinate()
AttributeError: 'NoneType' object has no attribute 'coordinate'
Apple's developer documentation isn't helping me out since my Objective-C isn't that strong.
You should wait until locationd
prompts for location access. Then allow python to use location services. I added a wait block to your code:
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)
The prompt will be shown while the loop is waiting for authorizationStatus
and getting a value in location
. Sometimes it takes about 30 seconds to show the dialog:
If the user accepts the access by choosing "Allow", the value of authorizationStatus
becomes 3. After that, the loop will be continued to wait until a location value obtained.
Unfortunately, controlling the access via the Security and Privacy section in System Preferences will be impossible sometimes. Because of disappearing the python row in Location Services list, you cannot check or uncheck it. Also, the tccutil
shell command cannot control this.
If you accidentally chose the "Don't Allow" button, it can be reset with these instructions.
这篇关于CoreLocation AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!