【问题标题】:Location Manager not displaying Latitude, Longitude, or Altitude位置管理器不显示纬度、经度或海拔高度
【发布时间】:2014-10-26 07:57:00
【问题描述】:

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

然后,它采用纬度/经度并使用反向地理编码将它们转换为地址,然后也显示在标签中。

但是,使用我当前的代码,我无法让两者同时工作。那就是现在地理编码可以工作,但不会显示纬度/经度和海拔高度,如果我注释掉反向地理编码的部分,它们会显示得很好。

我一直在寻找解决方案,但没有找到任何可行的方法。任何建议将不胜感激。我认为这与两个 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

【问题讨论】:

    标签: ios gps cllocationmanager


    【解决方案1】:

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

    复制所有代码:

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

    进入这个:

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

    然后删除第一个。据我所知,如果你已经实现了第二个,系统不会费心调用第一个,因为它已被弃用,而且他们无论如何都会做同样的事情......

    【讨论】:

    • 感谢您的回复,所以当我尝试此操作时,我收到一条错误消息,提示“使用未声明的标识符 newlocation,并建议通过将其设为 CLLocation 来修复它。当我这样做时,它会显示该属性在对象类型“cllocation”上找不到坐标
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 2012-04-11
    • 2021-09-15
    • 2016-01-11
    相关资源
    最近更新 更多