为什么我的iPhone应用程序首次启动时会获得额外的位置坐标点?

时间:2011-10-10 09:45:57

标签: objective-c core-location cllocationmanager mapkit mkmapview

我正在使用一个使用CLLocationManager的iPhone应用程序。当用户进行运行时,它会在mapView上显示运行路径。我正在使用以下代码在mapView上绘制运行路径:

 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]; 

//  }
}
 }
 }

问题在于,当我开始跑步时,我得到一些额外的分数,然后由于这些点,我在mapView上得到一条无法反映实际运行的无关线。它甚至发生在我在iPhone上安装应用程序并首次运行时。我不知道为什么它会增加这些额外的积分。任何人都可以帮助我吗?提前谢谢。

1 个答案:

答案 0 :(得分:2)

您获得的第一个位置通常是缓存位置且已过时。您可以检查位置的年龄以及是否旧(> 60秒或其他),然后忽略该位置更新。请参阅此答案here

- 编辑 - 如果您仍然遇到问题,请将此代码放入didUpdateToLocation:并向我们展示NSLog的实际输出(您可以编辑您的问题并添加输出):

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