簡體   English   中英

在IOS7中獲取經度和緯度

[英]Getting latitude and longitude in IOS7

我正在開發一個使用定位服務並在后台運行的iPhone應用程序。此應用程序在IOS6上運行時會准確顯示緯度和經度。但是如果我在IOS7上運行同一應用程序,則有時會顯示緯度和經度,有時是-1和-1。 我不了解IOS7上的這種行為。 這種行為的原因可能是什么?

為了模擬位置,請選擇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);
}

使用此方法獲取位置連續使用此方法

-(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中棄用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM