Start a new topic
Answered

get the current location from KCSEntityKeyGeolocation

Hi,


I am updating the location constantly.. how I want to get the current location in Swift


let geoloc = resultProfile.getValueForAttribute(KCSEntityKeyGeolocation) as? NSArray


how can I now use locationFromKinveyValue:   or is there a different way to get the CLLocation out of my the KCSEntityKeyGeolocation 


thanks,



Best Answer

Hi Pranav


While I was trying to get the info you requested, I found the issue. 


I didn't realise the IOS geocoder.geocodeAdressString func was asynchronous. Rookie mistake - sorry for wasting your time! (But grateful for your prompt as it made me see my error.)


Thanks for your help.


Ramesh


Hello Peter, 


Have you checked the Location guide in the iOS section:  http://devcenter.kinvey.com/ios/guides/location  


Based on the code there 


let event = Event()

if CLLocationManager.locationServicesEnabled() { 

let manager = CLLocationManager() //... set up CLLocationManager and start updates 

let currentLocation = manager.location 

event.geocoord = currentLocation

}


It looks like you can call manager.location to get your current location using the code above.


Please let me know if this helped with your confusion,


Thanks,

Ramesh,

  1. Can you let me know the result of print("placemark : \(geoCoord!)”) in both the cases - successful as well as unsuccessful?
  2. Also are you getting any error when trying to save the entity? A screenshot will help.

 

Thanks,

Pranav

Kinvey

Peter,

I wanted to check whether the suggestion has helped resolve your issue.

Thanks,
Pranav

Kinvey Support

hi there, Pranav. I have related question on "KCSEntityKeyGeolocation" - I'm trying to send an address as a CLLocation / KCSEntityKeyGeolocation to the backend. This doesn't seem to work.


I can only load the user's current location, and (looks like) no other CLLocation variables. Example of the code that doesn't work, followed by the code that works below. Any ideas on how I can get around this?


Thanks for your help.


Ramesh


Code that doesn't works

geocoder.geocodeAddressString(fullAddress, completionHandler: {(placemarks, error) -> Void in

 if((error) != nil) {

 print("Reverse geo coord error: \(error)")

 }


 if let placemark = placemarks?.first {

 let geoCoord: CLLocation?

 geoCoord = placemark.location

 print("placemark : \(geoCoord!)")

 event.geocoord = geoCoord // <-- this doesn't load to the backend

 }

 })


Code that works 

let manager = CLLocationManager()

 let currentLocation = manager.location

 event.geocoord = currentLocation


For both the above, event.geocoord is the KCSEntityKeyGeolocation variable

Hello Ramesh,


The first thing to do is check your query to make sure the the _geoloc coordinates are being set up in the proper [long,lat] format instead of the more common [lat,long] format. If you are attempting to insert _geoloc data using the incorrect [lat,long] format, then the query will fail and return an error. I found a several errors for your app in our server logs with the following related error that was returned to your app.


"planner returned error: unable to find index for $geoNear query"


The [long,lat] format is a requirement of our backend MongoDB database. Please see our DevCenter documentation regarding this at the following URL.


http://devcenter.kinvey.com/ios/reference/op-intel/reference.html#Geolocationfields


If you find that you are, in fact, using the incorrect [lat,long] format, please make the necessary changes to swap the fields and let us know whether or not that fixes this problem. If this suggestion doesn't resolve the problem, we will be happy to help you investigate further. 


Pranav will also take a look at this problem today and he may have some additional suggestions.


Regards,


Billy Gee

Kinvey Support


Hi Ramesh,


Your earlier reply opened a new ticket on portal. So I am just copying your reply below to update the Forum issue.


"""

Hi Billy 


Thank you for the quick reply. I will have a go at it and get back to you. 


It's a bit weird if the ios current location and placemark information is presented differently (they are both cllocation type). But I'll have a look to confirm. 


Thanks again, 

Ramesh

"""


Regards,

Wani

Ramesh,

Did you get a chance to confirm the lat-long format in your code?

 

Thanks,

Pranav

Kinvey

Answer

Hi Pranav


While I was trying to get the info you requested, I found the issue. 


I didn't realise the IOS geocoder.geocodeAdressString func was asynchronous. Rookie mistake - sorry for wasting your time! (But grateful for your prompt as it made me see my error.)


Thanks for your help.


Ramesh

Ramesh,

Glad it is working for you now.

 

Thanks,

Pranav

Kinvey

Hi Pranav I checked, and both the data I was loading is in the same format (the one that worked and didn't work). At least, as far as I can see the both are Cllocation. Any other ideas I could try? I must be missing something. Ramesh
Login or Signup to post a comment