How to wait until location is completely found? (Core Location)

巧了我就是萌 提交于 2019-12-03 00:11:09

Core Location often call didUpdateToLocation with location detected durning previous session. So you can just skip first location that it send to you. And if you're calculating user speed based on this data you should to be aware of that behavior. Pedestrians at 100mph is usual in that case. :)

If you're submitting comments or photos with geo coordinates - start receiving geo coordinates when user did enter to write a comment screen. while he will type a message - detected location become pretty accurate.

I don't see how you expect this to work. There's no guarantee that you will ever obtain a location with a specific level of accuracy. You're assuming that you will eventually get a location that has the reported accuracy you desire and that won't necessarily happen. LocationManager will send you a sequence of location updates which are generally, but certainly not always, increasingly accurate. Unfortunately you can't demand that the device receive a signal strong enough for it to give you a position of arbitrary accuracy and there's no way the device can predict the future to say "this is the most accurate position I'm going to get".

You might try storing all the locations reported over some time interval or until the reported accuracy seems to stabilize and accept that after several seconds you're probably not going to get a significantly more accurate position. You could then use the last reported position, or the most accurate, or a weighted center point of all of them, or whatever you think makes sense.

Given that you say this is going to be used to perform a reverse geocoding to get a zipcode I have to wonder why this is a concern. Even the most granular position is probably going to hit the user's current zipcode and even the most accurate will never be exactly right in every case so you'll want to allow users to correct it anyway if this is visible to your users.

So you can really see what's happening, try NSLog'ging the horizontalAccuracy property of your newLocation object. You'll see that it's narrowing in on the location with each hit.

That means you can then TEST for the horizontal accuracy when your location manager gives you an update, and discard those that are too inaccurate for your use.

NSLog() the oldLocation property, I think it should (or shouldn't?) be nil. and you can hide the userLocation if something failed:

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    mapView.showsUserLocation = NO;
}

//EDIT: the desiredAccuracy says that the didUpdateToLocation only gets called if you move that far away from the old position. If you set the desiredAccuracy to 100m it will only get called when the user moves 100m away from his old position.

Save off the launch (or wake) time of your application, and discard any results from location manager that are older than the time your app was launched. CLLocation has a date stamp on it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!