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

发布于 2024-12-08 18:30:13 字数 2727 浏览 1 评论 0原文

我正在开发一个使用 CLLocationManager 的 iPhone 应用程序。当用户跑步时,它会在地图视图上显示跑步路径。我使用以下代码在 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 上安装该应用程序并首次运行时也会发生这种情况。我不知道为什么要添加这些额外的积分。有人能帮我吗?提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居者 2024-12-15 18:30:13

您获得的第一个位置通常是缓存位置并且是旧的。您可以检查位置的存在时间,如果它是旧的(> 60 秒或其他),则忽略该位置更新。请参阅此处的答案。

--编辑-- 如果您仍然遇到问题,请将此代码放入 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); 

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); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文