2014-09-02 50 views
0

我有一個應用程序,在詢問用戶位置後,標籤中顯示緯度/長度和高度。位置管理器不顯示緯度,長度或海拔高度

然後它採用緯度/經度並使用反向地理編碼將它們轉換爲地址,然後將其顯示在標籤中。

但是,用我現在的代碼,我無法同時工作。這是現在地理編碼將工作,但經緯度和高度將不會顯示,如果我註釋了反向地理編碼的部分,他們顯示很好。

我找了一個解決方案,並沒有找到任何工作。任何建議將不勝感激。我認爲它與兩個updatelocation方法有關,但我無法弄清楚要更改什麼。

#import "LocationDataViewController.h" 
#import "AppDelegate.h" 
#define METERS_PER_MILE 1609.344 

@interface LocationDataViewController() 
@property (nonatomic, retain) IBOutlet UIScrollView* scrollView; 
@end 



@implementation LocationDataViewController 
@synthesize popoverController; 
@synthesize libraryButton; 
@synthesize cameraButton; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    //gets current location as soon as user allows location sharing 
    _locationManager = [[CLLocationManager alloc] init]; 
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    _locationManager.delegate = self; 
    [_locationManager startUpdatingLocation]; 
    _startLocation = nil; 

    } 



- (void)viewWillDisappear:(BOOL)animated 
{ 



} 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)onBack:(id)sender 
{ 
    [self.navigationController popViewControllerAnimated:YES]; 
} 




//Displays lat/long 

#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 *currentAltitude = [[NSString alloc] 
           initWithFormat:@"%+.6f", 
           newLocation.altitude]; 
    _altitude.text = currentAltitude; 

    AppDelegate *appDelegate = 
    [[UIApplication sharedApplication] delegate]; 
    appDelegate.strLocationLat = currentLatitude; 
    appDelegate.strLocationLong = currentLongitude; 


} 


-(void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error 
{ 
} 

//turns lat/long into address 

#pragma mark - actions 

- (IBAction)findCurrentAddress:(id)sender 
{ 
    if([CLLocationManager locationServicesEnabled]) 
    { 
     if(_locationManager==nil) 
     { 
      _locationManager=[[CLLocationManager alloc] init]; 
      _locationManager.distanceFilter = 500; 
      _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; 
      _locationManager.delegate = self; 

     } 

     [_locationManager startUpdatingLocation]; 
     self.address.text = @"Getting location..."; 
    } 
    else 
    { 
     [email protected]"Location services are unavailable"; 
    } 
} 

#pragma mark - delegate methods for geocoder 

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

    // Make sure this is a recent location event 
    CLLocation *newLocation = [locations lastObject]; 
    NSTimeInterval eventInterval = [newLocation.timestamp timeIntervalSinceNow]; 
    if(abs(eventInterval) < 30.0) 
    { 
     // Make sure the event is valid 
     if (newLocation.horizontalAccuracy < 0) 
      return; 

     // Instantiate _geoCoder if it has not been already 
     if (_geocoder == nil) 
      _geocoder = [[CLGeocoder alloc] init]; 

     //Only one geocoding instance per action 
     //so stop any previous geocoding actions before starting this one 
     if([_geocoder isGeocoding]) 
      [_geocoder cancelGeocode]; 

     [_geocoder reverseGeocodeLocation: newLocation 
         completionHandler: ^(NSArray* placemarks, NSError* error) 
     { 
      if (placemarks && placemarks.count > 0) 
      { 
       CLPlacemark *placemark = placemarks[0]; 

       NSDictionary *addressDictionary = 
       placemark.addressDictionary; 


       self.address.text =[addressDictionary 
            objectForKey: 
            (NSString *)kABPersonAddressStreetKey]; 

       self.city.text = [addressDictionary 
            objectForKey: 
            (NSString *)kABPersonAddressCityKey]; 


       self.state.text = [addressDictionary 
            objectForKey: 
            (NSString *)kABPersonAddressStateKey]; 


       self.zip.text = [addressDictionary 
            objectForKey: 
            (NSString *)kABPersonAddressZIPKey]; 


      } 
     }]; 

     //Stop updating location until they click the button again 
     [manager stopUpdatingLocation]; 
    } 
} 





@end 

回答

0

您同時使用一個過時的方法和不同事物的新方法......

中複製所有代碼:

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

進入這一個:

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

然後刪除第一個。據我所知,如果你有第二個實施,系統不會打擾呼籲第一個,因爲它已被棄用,他們也做同樣的事情無論如何...

+0

感謝您的回覆,所以當我嘗試這我得到一個錯誤,說「使用未聲明的標識符newlocation,並建議通過使其CLLocation修復它。當我這樣做,然後它說,屬性cooridnate找不到對象類型'cllocation' – user3927993 2014-09-03 02:23:44

+0

想通了謝謝 – user3927993 2014-09-03 03:32:43