iOS Compass App:位置坐标不断变化。如何稳定它?

时间:2014-07-16 05:24:03

标签: ios iphone

我刚刚完成指南针应用程序,它将显示2个坐标之间的距离。 这是工作代码:

....

 // created a timer to call locationUpdate method : 5sec
[NSTimer scheduledTimerWithTimeInterval:5 target: self selector:  @selector(locationUpdate) userInfo: nil repeats: YES];
....

-(void)locationUpdate {

[locationManager startUpdatingLocation];

}

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLoc
      fromLocation:(CLLocation *)oldLoc
 {
//To get the current location Latitude & Longitude.
float latitude = locationManager.location.coordinate.latitude;
float longitude = locationManager.location.coordinate.longitude;

startPoint = [[CLLocation alloc] initWithLatitude: latitude  longitude: longitude ]; //Current Latitude and Longitude
endPoint = [[CLLocation alloc] initWithLatitude: 12.923670 longitude: 77.573496]; //Target Latitude and Longitude   -----------------------------------> Need to come from database.

//To get the distance from the 2 coordinates in feet
CLLocationDistance distInMeter =  [startPoint distanceFromLocation:endPoint];

//Lable veiw to update remaining distance. 
if(distInMeter > 999.000f) {
    self.labelLongLat.text = [NSString stringWithFormat: @"Remainig distance %.2f KM with Lat : %lf  LAN %lf", distInMeter / 1000, latitude, longitude ];
}else
self.labelLongLat.text = [NSString stringWithFormat: @"Remainig distance %.2f M Lat : %lf  LAN %lf" , distInMeter, latitude,longitude ];
 }

我的问题是在每5秒更新位置时,坐标变化很大。这将导致Remaining distance计算。哪个非常不稳定!!我该如何解决这个问题?

先谢谢,

1 个答案:

答案 0 :(得分:1)

不推荐使用您正在使用的委托方法。您应该使用locationManager:didUpdateLocations:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{

    CLLocation *currentLocation = [locations lastObject];

    endPoint = [[CLLocation alloc] initWithLatitude: 12.923670 longitude: 77.573496]; //Target Latitude and Longitude   -----------------------------------> Need to come from database.

    //To get the distance from the 2 coordinates in meters

    CLLocationDistance distInMeter =  [currentLocation distanceFromLocation:endPoint];

    //Label view to update remaining distance. 
    if(distInMeter > 999 ) {
        self.labelLongLat.text = [NSString stringWithFormat: @"Remaining distance %.2f Km with Lat : %lf  LAN %lf", distInMeter / 1000, currentLocation.coordinate.latitude, currentLocation.coordinate.longitude ];
    } else {
        self.labelLongLat.text = [NSString stringWithFormat: @"Remaining distance %.2f m Lat : %lf  LAN %lf" , distInMeter, currentLocation.coordinate.latitude, currentLocation.coordinate.longitude ];
    }
 }

您所在位置的准确性会受到信号质量(高层建筑,室内位置等)的影响。您可以检查您所在位置的horizontalAccuracy属性,以了解该位置的准确程度。如果准确度低,则可以推迟更新标签。请注意,您可能永远无法获得准确的修复。

一种策略是等待精确度<20m或5次更新后

@property (nonatomic) NSInteger locationUpdates;

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{

   self.locationUpdates++;

   CLLocation *currentLocation = [locations lastObject];

   if (currentLocation.horizontalAccuracy < 20.0 || self.locationUpdates>5) {

      endPoint = [[CLLocation alloc] initWithLatitude: 12.923670 longitude: 77.573496]; //Target Latitude and Longitude   -----------------------------------> Need to come from database.

//To get the distance from the 2 coordinates in meters

      CLLocationDistance distInMeter =  [currentLocation distanceFromLocation:endPoint];

//Label view to update remaining distance. 
       if(distInMeter > 999 ) {
        self.labelLongLat.text = [NSString stringWithFormat: @"Remaining distance %.2f Km with Lat : %lf  LAN %lf", distInMeter / 1000, currentLocation.coordinate.latitude, currentLocation.coordinate.longitude ];
      } else {
         self.labelLongLat.text = [NSString stringWithFormat: @"Remaining distance %.2f m Lat : %lf  LAN %lf" , distInMeter, currentLocation.coordinate.latitude, currentLocation.coordinate.longitude ];
      } 
   } 
}