位置管理器不显示纬度,经度或海拔高度

时间:2014-09-02 06:03:57

标签: ios gps cllocationmanager

我有一个应用程序,在询问用户位置后,会在标签中显示纬度/经度和高度。

然后使用lat / long并使用反向地理编码将它们转换为一个地址,然后在标签中显示。

但是,使用我当前的代码,我无法同时使用它们。现在就是地理编码可以工作,但是不会显示纬度/经度和海拔高度,如果我在反向地理编码中注释掉它们,它们会显示正常。

我找了一个解决方案但没找到任何工作。任何建议将不胜感激。我认为它与两个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
    {
        self.address.text=@"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

1 个答案:

答案 0 :(得分:0)

您正在使用不推荐使用的方法和新方法来处理不同的事情......

复制所有代码:

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

进入这一个:

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

然后删除第一个。据我所知,如果你已经实现了第二个,系统将不会打扰第一个,因为它已被弃用,他们也会做同样的事情......