How to get latitude and longitude from a CLLocationCoordinate2D
Sorry for my poor English!
I wrote a script on Pythonista to get my current coordinate within CLLoctionManagerDelegate’s method.
The coordinate of type CllocationCorrdinate2D is a structure.I don’t know how to get the latitude and latitude.Have Someone ever do that before?Thx for you reply!
The following code is what I did right now,and it doesn’t get the right result:
from objc_util import * from location import * class CLLocationCoordinate2D (Structure): _fields_ = [('latitude', c_double), ('longitude', c_double)] def locationManager_didUpdateHeading_(_self,_cmd,manager,newheading): heading=newheading #print(heading) def locationManager_didUpdateLocations_(_self,_cmd,manager,locations): locations=ObjCInstance(locations) print(locations.coordinate().latitude) manager=ObjCInstance(manager) manager.stopUpdatingLocation() methods=[locationManager_didUpdateHeading_,locationManager_didUpdateLocations_] protocols=['CLLocationManagerDelegate'] LMD=create_objc_class('Delegate',NSObject,methods=methods,protocols=protocols) CLLocationManager=ObjCClass('CLLocationManager') locationManager=CLLocationManager.alloc().init() delegate=LMD.alloc().init() locationManager.setDelegate(delegate) #locationManager.startUpdatingHeading() locationManager.startUpdatingLocation() coor=CLLocationCoordinate2D(30.1,104) #print(coor.latitude)
What are you getting? All 0's?
It seems from your code that you get a single location, then stop updating. You might want to let the manager run a few cycles -- and/or use the last entry in locations (locations[-1]) rather than the first (last is most recent). I know in the
locationmodule, sometimes it takes one or two samples to get valid entries.
Also, per Apple, you might need to
Configure the properties related to the service you intend to use. For example, when getting location updates, always configure the distanceFilter and desiredAccuracy properties.
See CLLLocationManager docs.
Of course, if you just want locations, I assume you know you can use the pythonista
locationmodule which does this all for you (but perhaps without the same level of control)
Thx for your reply.I think I’ve configured the CLLocationManager and it’s delegate rightly to get the location.But I don’t know how to get CLLocationCoordinate2D’s latitude and longitude.(I mean I don’t know how to get a property from a structure).That makes me have a bee my head.
I use the Location of Pythonista to get the location,But I still want to know how use the native core location to do the same work.
its helpful to mention if you get an error, and paste the traceback. In this case:
AttributeError: '__Structure' object has no attribute 'latitude'would have led in the right direction.
Long story short, objc_util doesnt always know how to name the structure fields that are return types -- in this case, it only knows it is a structure with two doubles, so names them a and b.
You can specify the return type and argtype, or, you could cast the returned anonymous structure as the one you want.
coordinates=locations.coordinate(restype=CLLocationCoordinate2D, argtypes=) print(coordinates.latitude, coordinates.longitude)
Thanks a lot!That’s great!