0

我正在开发一个使用定位服务并在后台运行的 iPhone 应用程序。该应用程序在 IOS6 上运行时准确显示纬度和经度。但如果我在IOS7运行相同的应用程序,有时它会准确显示纬度和经度,有时是 -1 和-1。我不理解IOS7上的这种行为。这种行为的原因可能是什么?

4

2 回答 2

0

为了模拟位置,请选择 iOS Simulator Debug -> Location 菜单选项,然后选择预定义的位置或旅程之一(例如 City Bicycle Ride),或 Custom Location... 以输入特定的纬度和经度。

这段代码对我有用。

创建 CLLocationManager 对象

下一个任务是实现代码以创建 CLLocationManager 类的实例。由于这需要在视图加载时发生,理想的位置是在 LocationViewController.m 文件中视图控制器的 viewDidLoad 方法中:

- (void)viewDidLoad {
    [super viewDidLoad];
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    _locationManager.delegate = self;
    [_locationManager startUpdatingLocation];
    _startLocation = nil;
}

实现动作方法

-(void)resetDistance:(id)sender
{
        _startLocation = nil;
}

实现 CLLocationManager 方法

#pragma mark -
#pragma mark CLLocationManagerDelegate

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
                  fromLocation:(CLLocation *)oldLocation
{
     NSString *currentLatitude = [[NSString alloc] 
                  initWithFormat:@"%+.6f", 
                  newLocation.coordinate.latitude];
     _latitude.text = currentLatitude;

     NSString *currentLongitude = [[NSString alloc] 
          initWithFormat:@"%+.6f",
          newLocation.coordinate.longitude];
     _longitude.text = currentLongitude;

     NSString *currentHorizontalAccuracy = 
              [[NSString alloc] 
             initWithFormat:@"%+.6f",
             newLocation.horizontalAccuracy];
     _horizontalAccuracy.text = currentHorizontalAccuracy;

     NSString *currentAltitude = [[NSString alloc] 
                  initWithFormat:@"%+.6f",                                                          
                  newLocation.altitude];
     _altitude.text = currentAltitude;

     NSString *currentVerticalAccuracy = 
              [[NSString alloc] 
              initWithFormat:@"%+.6f",
              newLocation.verticalAccuracy];
     _verticalAccuracy.text = currentVerticalAccuracy;

     if (_startLocation == nil)
            _startLocation = newLocation;

     CLLocationDistance distanceBetween = [newLocation
            distanceFromLocation:_startLocation];

     NSString *tripString = [[NSString alloc] 
           initWithFormat:@"%f", 
           distanceBetween];
     _distance.text = tripString;
}

最后试试这个方法

试试这个方法获取不断更新的经纬度

(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{  
    CLLocation *location_updated = [locations lastObject];   
    NSLog(@"updated coordinate are %@",location_updated);
}
于 2013-11-12T06:53:11.573 回答
0

使用此方法连续获取位置 使用此方法

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

{
location_updated = [locations lastObject];    
NSLog(@"updated coordinate are %@",location_updated);

}

可能你正在使用这种方法

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

此方法已在 iOS 6.0 中弃用

于 2013-11-12T08:08:44.687 回答