1

I am working an iPhone app which is using CLLocationManager. When a user goes for a run, it shows the run path on a mapView. I am drawing the running path on mapView using following code:

 double leastDistanceToRecord = 0.0000905;

 - (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (newLocation.horizontalAccuracy >= 0) {
    if (!runoPath)
    {
        NSLog(@"in !runoPath if");
        // This is the first time we're getting a location update, so create
        // the RunoPath and add it to the map.
        runoPath = [[RunoPath alloc] initWithCenterCoordinate:newLocation.coordinate];
        [map addOverlay:runoPath];

        self.currentRunData = [[RunData alloc] init];

        [currentRunData startPointLocation:newLocation];    

        // On the first location update, zoom map to user location
        MKCoordinateRegion region = 
        MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1000, 1000);
        [map setRegion:region animated: NO];


    }
    else
    {
        // This is a subsequent location update.
        // If the runoPath MKOverlay model object determines that the current location has moved
        // far enough from the previous location, use the returned updateRect to redraw just
        // the changed area.

        double latitudeChange = fabs(newLocation.coordinate.latitude - oldLocation.coordinate.latitude);
        double longitudeChange = fabs(newLocation.coordinate.latitude - oldLocation.coordinate.longitude);
        if (latitudeChange > leastDistanceToRecord || longitudeChange > leastDistanceToRecord) {
            MKMapRect updateRect = [runoPath addCoordinate:newLocation.coordinate];
            if (!MKMapRectIsNull(updateRect))
            {
                // There is a non null update rect.
                // Compute the currently visible map zoom scale
                MKZoomScale currentZoomScale = map.bounds.size.width / map.visibleMapRect.size.width;
                // Find out the line width at this zoom scale and outset the updateRect by that amount
                CGFloat lineWidth = MKRoadWidthAtZoomScale(currentZoomScale);
                updateRect = MKMapRectInset(updateRect, -lineWidth, -lineWidth);
                // Ask the overlay view to update just the changed area.
                [runoPathView setNeedsDisplayInMapRect:updateRect];
            }
        //  [currentRunData updateLocation:oldLocation toNewLocation: newLocation];         
        }
        [currentRunData updateLocation:oldLocation toNewLocation: newLocation]; 

//  }
}
 }
 }

The problem is that when I start a run, I get some extra points and then because of those points I get an extraneous line on mapView that does not reflect the actual run. It even happens when I install the app on my iPhone and run it for the first time. I don't know why it's adding those extra points. Can anyone help me with that? Thanks in advance.

Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
Piscean
  • 3,069
  • 12
  • 47
  • 96

1 Answers1

2

The first location you get is usually a cached location and is old. You can check the age of the location and if it is old (>60 seconds or whatever) then ignore that location update. See this answer here.

--EDIT-- If you are still having problems, put this code in didUpdateToLocation: and show us the actual output from NSLog (you can edit your question and add the output):

NSTimeInterval age = -[newLocation.timestamp timeIntervalSinceNow]; 
NSLog(@"age: %0.3f sec, lat=%0.2f, lon=%0.2f, hAcc=%1.0f", 
      age, newLocation.coordinate.latitude, newLocation.coordinate.longitude,
      newLocation.horizontalAccuracy); 
Community
  • 1
  • 1
progrmr
  • 75,956
  • 16
  • 112
  • 147
  • sometimes those points are still coming. i wrote these line of code: NSTimeInterval ageInSeconds = -[newLocation.timestamp timeIntervalSinceNow]; NSLog(@"ageInSeconds: %d", ageInSeconds); if ((ageInSeconds >= 0.0) && (ageInSeconds < 10.0)){ // do stuff } sometimes that log shows ageInSeconds as a big -ve number. but still sometimes i am getting those point. – Piscean Oct 11 '11 at 09:06
  • What is a "-ve" number? Why can't you just show the log output in your question so we don't have to guess? – progrmr Oct 11 '11 at 12:27
  • "-ve" is negative. sorry for now writing whole word. Now it is working fine. I dont know why it was giving negative number. thanks – Piscean Oct 11 '11 at 14:16
  • age is a float and you were printing it using %d which is for int, thus it didn't print correctly. – progrmr Oct 11 '11 at 14:50