开发者

Why do I get extra location coordinate points when my iPhone app first launches?

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.lo开发者_高级运维ngitude);
        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.


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); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜