【发布时间】: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