简体   繁体   中英

Retrieving current user location from mapView, without map being on screen

I'm writing an app that stores the location of the places you have been throughout the day. I've done a lot of research on CLLocationManager, and have been testing my app for the last 3 weeks. The locations I get with CLLocationManager sometimes is very inaccurate, sometimes 4 or 5 miles away from where I am. I have a log in my test app and realized that every time I get a new location my DidUpdateToLocation method is called 3 times in a row, like within one second, and I get 3 different locations. One of them is always right, but the other 2 are off. I've tested with it with different accuracies(hundredMeters, nearestTenMeters, and Best), but still had the same problem. So, my first question is:

Is there anyway I can find out which of these locations is the right one so I can store it?

I've also realized that the user's current location on the map view is always very accurate, so I thought about getting the user's location from the mapView(blue dot) instead of the CLLocationManager, but I learned that a mapView object will only return the current location if the map is actually on screen, and this is not the case since I want my app to run in the background. So the second question is:

Is there anyway to get the user's current location from a mapView without the map being on screen? Or at least use the same technique the mapView uses for finding the current location?

If you have any experience with mapKit and CLLocationManager, please share your thoughts.

Thanks for you time. Any help is appreciated.

Take a look at Apple's sample code Locate Me. It uses the variable:

CLLocation *bestEffortAtLocation;

to determine upon updates if the location is less than the last location and less than a time limit set until the most accurate location is found. In particular look at the GetLocationViewController in the sample code. I've used this code in my apps and it works great.

https://developer.apple.com/library/ios/#samplecode/LocateMe/Listings/Classes_GetLocationViewController_m.html#//apple_ref/doc/uid/DTS40007801-Classes_GetLocationViewController_m-DontLinkElementID_8

Is there anyway I can find out which of these locations is the right one so I can store it?

Look at the horizontalAccuracy property of the locations that you receive. Don't use locations that are less accurate than what you're looking for.

I've also realized that the user's current location on the map view is always very accurate

The map view probably doesn't have any special access to API's that are more accurate than what you've got, it just uses them better. Instead of starting from scratch each time you need a fix, try caching the location and then updating when you get a new location that's sufficiently accurate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM