2015-10-07 89 views
0

我想多次更新當前位置。 在我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]; 
} 

它不是自動更新當前的位置,

請幫幫我!

+2

無關,但從來沒有做iOS版本檢查。檢查是否存在類或方法。 – rmaddy

+0

該委託方法也被棄用 - 使用'didUpdateLocations:'您是否從最初位置移動了一公里以上? – Paulw11

+0

使用 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations方法。 Paulw11你能幫我解決一下這個問題嗎? – user5068763

回答

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並祝:

#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