2

In Xcode 4.5 apple introduced apple new maps. My application heavliy requires map services. And I have noticed in my application it shows the wrong current location until you delete the app and reopen it shows the right current location (Sometimes it doesn't). Just to mention I was connected to 4G when it show the wrong current location. Is there a way to fix this bug because my application heavily needs the right location. Thanks in advance. If you could provide code it would help a lot...Edit: Same Issue in Apple Map App

My Code:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    {

    }

    if (!oldLocation)
        totalDistance = 0.0;
    else
        totalDistance += [newLocation distanceFromLocation:oldLocation];

}
  • I have noticed this issue on one of the apps I am working on which requires geo fencing. It displays the location it had locked on to previously, even though I have moved more than a few miles (while the app was in background). – Nitin Alabur Nov 26 '12 at 04:00
  • How are you getting the location? CoreLocation actually caches the last known location and will give that cached location to you when you start up the app again. If you are using the delegate methods then you should check the timestamp and throw it away if it is too old. – sosborn Nov 26 '12 at 09:00
  • I am using corelocation, how can we delete the cached location :) –  Nov 26 '12 at 18:05
  • And I want to mention if the app is inbackground and then open its shows some crazy location that we didn't go to and then after 5 sec it shows the right location, how do we fix that... –  Nov 26 '12 at 22:24
  • You don't delete the cached location, you just ignore it until a valid location come up. And you do it just like I said - check the time stamp. If it is an old time stamp then ignore it until a recent timestamp comes up. – sosborn Nov 26 '12 at 23:53
  • But my app needs the precise current location when app is opened :(… –  Nov 27 '12 at 00:55

3 Answers3

2

The old approach from apple docs seems still working in iOS6 (didn't notice this in my active app (it tracks user's route via gps))

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;
    if (newLocation.horizontalAccuracy < 0) return; 

    // proceed with coords here
}

UPDATE from the discussion: Calculating total and current distance could be done like this (excluding some minor stuff):

// somewhere at the top
CLLocation* lastUsedLocation = nil; // last location used in calculation
CLLocation* pointA = nil;  // start of the track
double totalDistance = 0;  // total distance of track
double currentDistance = 0; // distance between startTrack point and current location
...

// when you start updating location:
- (void) startTracking {
    lastUsedLocation = nil;
    pointA = nil;
    totalDistance = 0;
    currentDistance = 0;
    [locationManager startUpdatingLocation];
}
...


// location update callback
 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 
    if (locationAge > 5.0) return;  // filter cached
    if (newLocation.horizontalAccuracy < 0) return; // filter invalid

    if(!pointA) pointA = [newLocation retain]; 

    if(lastUsedLocation) 
    { 
        totalDistance += [newLocation distanceFromLocation:lastUsedLocation]; 
    } 
    currentDistance = [pointA distanceFromLocation:newLocation]; 
    [lastUsedLocation release]; 
    lastUsedLocation = [newLocation retain]; 
}

If you need the option to turn off background location on purpose you disable it manually like:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    if(backgroundLocationDisabled)
    {
        [locationManager stopUpdatingLocation];
        // additional stuff
    }
}
MANIAK_dobrii
  • 6,014
  • 3
  • 28
  • 55
  • And also is your active app in the app store because @Scott told me apps in the app store don't have this bug.... –  Nov 28 '12 at 20:36
  • That depends on environment conditions at the moment, but this little filter does it good. My app is still in development, and I work on it every day using Xcode 4.5. – MANIAK_dobrii Nov 29 '12 at 03:54
  • Your code is great :) but I noticed that when app goes in background its shows the wrong current location when you open it again…… –  Nov 29 '12 at 04:56
  • My app supports background location, i.e. it tracks even in background. This one filters cached frames (locationAge stuff) and invalid frames (horisontalAccurace), others a used further. I'm not adding other trivial filters (min accuracy and others) due to it depends alot and some users may not gain even any of data. Except I skip first 3 frames to smooth "accuracy-enchancment" phase at the beginning of the track. I'm going to add some advanced filtering later if my customer desires. But this works good if GPS coverage is good, i.e. frames have good constant accuracy. Good complexity/benefit. – MANIAK_dobrii Nov 29 '12 at 06:19
  • 1
    You just need to add "App registers for location updates" to "Required background modes" in your project's Info tab. And that's all, those location update routines will be called even in background. Note that you'll have to add a note to your app description about that like "app eats your battery due to background location" or it will not pass the review. To save power and avoid this you may use significant location update, but it's not suitable for accurate location tracking. – MANIAK_dobrii Nov 29 '12 at 06:26
  • Could you show a tutorial (I am noting getting want you are saying :( –  Nov 29 '12 at 06:30
  • You may check this question http://stackoverflow.com/questions/10558569/ios-monitor-user-location-in-background. Just google "background location iOS" =) You may provide that option, but you'll have to turn location updates off manually when app goes to background. – MANIAK_dobrii Nov 29 '12 at 06:34
  • Sometimes in my application I notice my app shows the wrong user location on startup and then shows the right user location. How do I fix this issue... –  Nov 29 '12 at 06:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20302/discussion-between-maniak-dobrii-and-programmer) – MANIAK_dobrii Nov 29 '12 at 06:46
  • I fixed the NAN isue, but not the map view does't zomm in the current location ( or track the user) with your code... –  Nov 30 '12 at 03:15
1

you should check the timestamp .. if i understand your app logic correctly, you could do something like -

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {

NSDate* time = newLocation.timestamp;
    NSTimeInterval timePeriod = [time timeIntervalSinceNow];
    if(timePeriod < SOME_MINIMUM_TIME_PERIOD ) { //for eg: timePeriod < 1.0
               totalDistance += [newLocation distanceFromLocation:oldLocation];
    } else {
        // skip.. or do what you do on a 'location-not-updated' event
    }
}
Vinay W
  • 9,912
  • 8
  • 41
  • 47
  • Just one Question in the //skip.. or do what you do on a 'location-not-updated' event what should I put in there…. –  Dec 08 '12 at 01:08
  • I have quick question when there is no current location can I display an alert.... –  Dec 08 '12 at 17:14
  • the locations 0.0,0.0 is returned when the location cant be determined (because the gps is off or some other reason).. you can put in as a check and show an alert.. – Vinay W Dec 08 '12 at 20:44
  • if(timePeriod < SOME_MINIMUM_TIME_PERIOD ) { //for eg: timePeriod < 1.0 The Minimum Time Period –  Dec 08 '12 at 20:56
  • what about this line of code? are you asking what does this code do? – Vinay W Dec 08 '12 at 20:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20817/discussion-between-programmer-and-vinay-wadhwa) –  Dec 08 '12 at 21:04
  • Yes and does time period matter bigger the Number the more accurate the current location –  Dec 08 '12 at 21:05
0

I have noticed the same problem with Xcode 4.4+. The problem only occurs (randomly, or so it seems) within Xcode: if you upload the app to the App Store, this is not a problem anymore. In the meantime, please file a bug.

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80