简体   繁体   English

如何在iOS中获取“仅当前位置”?

[英]How to get 'current location only' in iOS?

I am using Core Location framework to getting location in iOS. 我正在使用Core Location框架在iOS中获取位置。 I use, CLLocationManager *locationManager; 我用的是CLLocationManager *locationManager; and call [locationManager startUpdatingLocation]; 并调用[locationManager startUpdatingLocation];

Problem is I only need instantaneous location but startUpdatingLocation keep on giving me location. 问题是我只需要瞬时位置,但是startUpdatingLocation继续给我位置。 To stop this I can use [self.locationManager stopUpdatingLocation] , but this look like a way out or hack. 要停止此操作,我可以使用[self.locationManager stopUpdatingLocation] ,但这看起来像是一种解决方法或破解方法。

Is there any better way to just get current location in iOS? 有没有更好的方法来获取iOS中的当前位置?

in your .h file 在您的.h文件中

#import <CoreLocation/CoreLocation.h>

 @interface LocationSearchViewController : UIViewController<UITextFieldDelegate,CLLocationManagerDelegate>
 {
 CLLocationManager *locationManager;
}

in your .m file 在您的.m文件中

- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];


locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

[locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
                           initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
[locationManager stopUpdatingLocation];   // when u got the lat and long it stop uoatde the location

CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {

  NSString  *getcurrlong = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
  NSSting *getcurrlat = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];



}
}

You can't think of location in those precise terms. 您无法精确地想到地理位置。 You'll get back increasingly accurate locations from the location manager and it's up to you to decide when it's accurate enough. 您会从位置管理器中获得越来越准确的位置信息,由您决定何时足够准确。 You have to weigh this against power usage and time. 您必须权衡功耗和时间。 The location updates will never be completely accurate. 位置更新永远不会完全准确。 The will never be returned immediately. 将永远不会立即返回。 The device has to power up hardware, then listen for GPS/WiFi/Cell signals and use all those to calculate location. 该设备必须打开硬件电源,然后收听GPS / WiFi / Cell信号并使用所有信号来计算位置。

There's no way to ask for "my precise current location" and have it given to you immediately. 无法要求“我的精确当前位置”并立即将其提供给您。 Location is not a property like [NSDate date] . 位置不是[NSDate date]这样的属性。 You can only ask for best-estimate location updates and they will only come to you in imprecise measurements, and never instantaneously (excepting cached location). 您只能要求获得最佳估计的位置更新,并且它们只会在不精确的测量中出现,并且绝不会即时(缓存的位置除外)出现。

Check it 核实

-(void)getCurrentLocation
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.delegate = self;
    [locationManager startUpdatingLocation];
    startLocation = nil;
}

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    if (startLocation == nil)
    {
        startLocation = newLocation;
        [locationManager setDelegate:nil];
        locationManager = nil;

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM