即使我不动,也会得到不同的位置

时间:2016-02-08 11:40:57

标签: ios objective-c geocoding cllocationmanager cllocation

我使用下面的代码来获取longitudelatitude,但即使我从我的位置移动,它也会给我不同的值

 locationmanager=[[CLLocationManager alloc]init];
    geocoder=[[CLGeocoder alloc]init];

    locationmanager.delegate=self;
    locationmanager.desiredAccuracy=kCLLocationAccuracyBest;
    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)
    {

        longitude=currentlocation.coordinate.longitude;
        latitude=currentlocation.coordinate.latitude;

    }
         [geocoder reverseGeocodeLocation:currentlocation completionHandler:^(NSArray *placemarks, NSError *error) {

          } ];

    }

请指导我,如果我从我的位置移动,我想要相同的位置。

2 个答案:

答案 0 :(得分:1)

蜂窝网络位置给出了手机存在的大致区域。它基于服务提供商塔。因此,当前位置将根据网络/ GPS位置跟踪/准确度自动更改。

我建议您使用距离过滤器获取位置,因此在您进入该区域之前,您当前的地址将与每个X米的仅位置更新保持一致。

locationManager.distanceFilter = 100;//Your own distance

即,

self.locationManager = [[[CLLocationManager alloc] init] autorelease];
    _locationManager.delegate = self;
    // This is the most important property to set for the manager. It ultimately determines how the manager will
    // attempt to acquire location and thus, the amount of power that will be consumed.
    _locationManager.distanceFilter = 100;
    // Once configured, the location manager must be "started".
    [_locationManager startUpdatingLocation];

希望这有帮助。

答案 1 :(得分:0)

在ViewController.h文件中:

@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
    CLLocationManager *location;
} 

在ViewController.m文件中:

- (void)viewDidLoad
{
     location = [[CLLocationManager alloc] init];
     location.delegate = self;
     location.distanceFilter = 100;
     [location requestWhenInUseAuthorization];
     [location requestAlwaysAuthorization];
     [location startUpdatingLocation];
}


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *locati = [locations lastObject];
    float lat=locati.coordinate.latitude;
    float lon=locati.coordinate.longitude;
    NSLog(@"%f %f",lat,lon);

    [location stopUpdatingLocation];
}

在您的info.plist中添加:

enter image description here