更新多次当前位置

时间:2015-10-07 03:57:08

标签: ios objective-c cllocationmanager

我想要多次更新当前位置。 在我的viewDidload方法中:

    locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.delegate = self;
    locationManager.distanceFilter = 1000.0f;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
        [locationManager requestWhenInUseAuthorization];
    }
    [locationManager startUpdatingLocation];

在我的didUpdateToLocation方法中,我记录了newlocation。

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    latitude = newLocation.coordinate.latitude;
    longitude = newLocation.coordinate.longitude;

    NSLog(@"Updated Location : lat--%f long--%f",latitude,longitude);
}

但它只记录一次。

我想多次更新当前。

====更新了我的代码:

-(void)getCurrentLocation{
    locationManager = [CLLocationManager new];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {
    // If it's a relatively recent event, turn off updates to save power.
    CLLocation* location = [locations lastObject];
//    NSDate* eventDate = location.timestamp;
//    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
//    if (abs(howRecent) < 15.0) {
        // If the event is recent, do something with it.
        longitude = location.coordinate.longitude;
        latitude = location.coordinate.latitude;
        [_lbllongtitude setText:[NSString stringWithFormat:@"%+.6f",longitude]];
        [_lbllatitude setText:[NSString stringWithFormat:@"%+.6f",latitude]];
//    }
    [self getLocationMapView];
}

它不是自动更新当前位置,

请帮帮我!

1 个答案:

答案 0 :(得分:0)

您可以通过更改属性来更新多时间位置。

这里是完整的源代码:

你的.h文件中的

    #import <UIKit/UIKit.h>

    #import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
    IBOutlet UIButton *btnGetLocation;
}
@property (strong, nonatomic) IBOutlet UILabel *lblsetLongitude, *lblsetLattitude, *lblsetAddress;
-(IBAction)GetLocation:(id)sender;
@end
你的.m fille中的

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()
{
    CLLocationManager *locationmanager;
    CLGeocoder *geocoder;
    CLPlacemark *placemark;
}

@end

@implementation ViewController
@synthesize lblsetAddress,lblsetLattitude,lblsetLongitude;
- (void)viewDidLoad
{
    [super viewDidLoad];

    locationmanager=[[CLLocationManager alloc]init];
    geocoder=[[CLGeocoder alloc]init];
   // placemark=[[CLPlacemark alloc]init];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(IBAction)GetLocation:(id)sender
{
    locationmanager.delegate=self;
    locationmanager.desiredAccuracy=kCLLocationAccuracyKilometer;
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        [locationmanager requestWhenInUseAuthorization];
    }
    [locationmanager startUpdatingLocation];

}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"error" message:@"Failed to get Location." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    CLLocation *currentlocation=newLocation;

    if (currentlocation!=nil) {
        self.lblsetLattitude.text=[NSString stringWithFormat:@"%.8f",currentlocation.coordinate.latitude];
        self.lblsetLongitude.text=[NSString stringWithFormat:@"%.8f",currentlocation.coordinate.longitude];
    }

    [geocoder reverseGeocodeLocation:currentlocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            lblsetAddress.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                                 placemark.subThoroughfare, placemark.thoroughfare,
                                 placemark.postalCode, placemark.locality,
                                 placemark.administrativeArea,
                                 placemark.country];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}
@end

通过更改属性,您可以获得多时间更新位置。

属性如:

kCLLocationAccuracyHundredMeters
kCLLocationAccuracyBestForNavigation
kCLLocationAccuracyKilometer