Ios 多次更新当前位置
我想多次更新当前位置。 在my viewDidload方法中:Ios 多次更新当前位置,ios,objective-c,cllocationmanager,Ios,Objective C,Cllocationmanager,我想多次更新当前位置。 在my viewDidload方法中: locationManager = [[CLLocationManager alloc] init]; locationManager.desiredAccuracy = kCLLocationAccuracyBest; locationManager.delegate = self; locationManager.distanceFilter = 1000.0f; if ([[[UIDe
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];
在我的didUpdateLocation方法中,我有log 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];
}
-(void)getCurrentLocation{
locationManager=[CLLocationManager新建];
locationManager.delegate=self;
locationManager.distanceFilter=KCLDistanceFilterOne;
locationManager.desiredAccuracy=KCallocationAccuracyBest;
[locationManager startUpdatingLocation];
}
-(无效)locationManager:(CLLocationManager*)经理
didUpdateLocations:(NSArray*)位置{
//如果是相对较新的事件,请关闭更新以节省电源。
CLLocation*location=[locations lastObject];
//NSDate*eventDate=location.timestamp;
//NSTimeInterval howRecent=[eventDate timeIntervalSinceNow];
//如果(abs(最近几年)<15.0){
//如果这件事是最近发生的,那就做点什么吧。
经度=位置。坐标。经度;
纬度=位置。坐标。纬度;
[[u lbllongtitude setText:[NSString stringWithFormat:@“%+.6f”,经度]];
[_lblatitysettext:[NSString stringWithFormat:@“%+.6f”,纬度]];
// }
[self-getLocationMapView];
}
它不是自动更新当前位置
请帮帮我 您可以通过更改属性来更新多次位置 以下是完整的源代码: 在您的.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
不相关,但从不检查iOS版本。检查类或方法是否存在。该委托方法也不推荐使用-使用
didUpdateLocations:
您是否已从初始位置移动了超过一公里?使用-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations方法。Paulw11你能帮我解决我的问题吗?你可以使用KCallocationAccuracyNearestTenMeters获得所需的准确度,当你的设备移出10米时,它会更新你的位置。每隔10米,它就会更新.tks mrunal thanki。我稍后再试。
#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