【问题标题】:CLLocationManager tracks wrong Location (Track Me)CLLocationManager 跟踪错误的位置(跟踪我)
【发布时间】:2013-01-02 22:52:54
【问题描述】:

我在我的代码中实现Track me 选项。CLLocationManager 没有按预期工作。当我启动应用程序时保持在同一位置,CLLocationManager 在 1 分钟内变化约 20-30 米..然后我保持不变。

如果我改变我的位置来跟踪同样的事情发生在开始 1 分钟 CLLocationManager 额外移动 20-30 分钟然后以我的速度移动..

为什么会这样..

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

  self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = 0.0001;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
 }


-(void)start {


[self.locationManager startUpdatingLocation];    
}


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

[self processLocationChange:newLocation fromLocation:oldLocation];

 }


 -(void)processLocationChange:(CLLocation*)newLocation fromLocation:oldLocation {

if (newLocation != oldLocation) {

    NSLog(@"Moved from %@ to %@", oldLocation, newLocation);

    CLLocation* lastKnownLocation = NULL;
    if ([self.locationPoints count] > 0) {
        lastKnownLocation = [self.locationPoints objectAtIndex:[self.locationPoints count] - 1];
    }
    else {
        lastKnownLocation = newLocation;
        self.bottomLeft = newLocation.coordinate;
        self.topRight = newLocation.coordinate;
    }

    // Check for new boundaries
    CLLocationCoordinate2D coords = newLocation.coordinate;
    if (coords.latitude < bottomLeft.latitude || coords.longitude < bottomLeft.longitude) {
        self.bottomLeft = coords;
        NSLog(@"Changed bottom left corner");
    }
    if (coords.latitude > topRight.latitude || coords.longitude > topRight.longitude) {
        self.topRight = coords;
        NSLog(@"Changed top right corner");
    }




    double speed = fabs(newLocation.speed);
    double deltaDist = fabs([newLocation distanceFromLocation:lastKnownLocation]);
    double newAvgSpeed = (self.totalDistance + deltaDist) / ((double)[self getElapsedTimeInMilliseconds] / 1000.0);
    double accuracy = newLocation.horizontalAccuracy;
    double alt = newLocation.altitude;

    NSLog(@"Change in position: %f", deltaDist);
    NSLog(@"Accuracy: %f", accuracy);
    NSLog(@"Speed: %f", speed);
    NSLog(@"Avg speed: %f", newAvgSpeed);




        self.totalDistance += deltaDist;
        self.currentSpeed = speed;
        self.avgSpeed = newAvgSpeed;
        self.altitude = alt;

        NSLog(@"Delta distance = %f", deltaDist);
        NSLog(@"New distance = %f", self.totalDistance);


        // Add new location to path
        [self.locationPoints addObject:newLocation];

        // Update stats display
        [self.first.start1 updateRunDisplay];

        // Update map view
        [self updateMap:lastKnownLocation newLocation:newLocation];

    }

}

【问题讨论】:

  • 你在哪里调用 -(void)start 方法。
  • 是的,它在 Viewcontroller 中..它工作正常..没有问题...问题出在位置上..
  • 注意:您使用的方法- (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation在iOS 6中已弃用。
  • 不,我使用了 iOs 5..iOs5 也给了我同样的变化
  • 好的。试试我在回答中建议的。应该能解决你的问题。我还编辑了问题的标题以便更好地搜索。

标签: iphone ios mkmapview cllocationmanager cllocation


【解决方案1】:

我认为让距离过滤器有效

self.locationManager.distanceFilter = kCLDistanceFilterNone;

您可以开始更新定位方法,但也可以尝试这种方法,这两种方法都是获取准确位置所必需的

[locationManager startMonitoringSignificantLocationChanges]; 

【讨论】:

    【解决方案2】:

    我在当前的计步器应用中遇到了同样的问题。我伸展,撞了我的头几天。然后我发现CLLocationManager 无法跟踪 self.locationManager.distanceFilter =2.0;,它给了我位置更新,即使设备是静止的。所以我只是将距离过滤器更改为 5.0 米,它开始工作得很好。尝试走 5 米,它应该可以工作,我进行了测试,我所有的错误通知问题都消失了:

      self.locationManager.distanceFilter =5.0;
    

    您正在使用self.locationManager.distancefilter=0.0001,我想CLLocationManager 无法跟踪这样一个小动作。您还需要过滤掉旧位置,即 Location Awareness Guide by Apple 中提到的缓存位置更新。我在我的代码中使用了这个条件来过滤所有超过 5 秒的事件。

    - (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
    {
       CLLocation *currentLocation=[locations lastObject];
       NSDate* eventDate = currentLocation.timestamp;
       NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    
       if(abs(howRecent)<5.0 && self.currentLocation.horizontalAccuracy<=10 && self.currentLocation.horizontalAccuracy>0)
       {
         //you have got fresh location event here.
       }
    }
    

    【讨论】:

    • 你做了同样的事情,但仍然改变了固定位置的位置。
    • 那么奇怪.. :/ 你设置locationManager.desiredAccuracy=kCLLocationAccuracyBest;了吗?
    • 你可以加我到skype patel.rohn
    猜你喜欢
    • 2016-07-01
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多