如何在目标c中从网络提供商处获取用户的当前位置?

时间:2016-08-17 08:47:45

标签: objective-c

在离线模式下如何获取用户在ios中的当前位置,是否可以通过网络提供商获取位置?

1 个答案:

答案 0 :(得分:1)

我给你完整的解决方案。如果你想实现这个目标,你应该使用核心位置框架

第一步:

  

在.h文件中导入CoreLocation框架

#import <CoreLocation/CoreLocation.h>

第二步:您需要在plist文件中添加以下内容

  

NSLocationWhenInUseUsageDescription字符串无论你想写什么,写在这里

     

NSLocationAlwaysUsageDescription字符串无论你想写什么,写在这里

     

隐私 - 位置使用说明字符串无论您想写什么,请写在这里

请看下面的屏幕截图

enter image description here

第三步:

  

添加CLLocationManagerDelegate

第四步:

  

创建CLLocationManager类的实例并存储强引用

@property (nonatomic , strong) CLLocationManager *locationManager;

为什么我们将CLLocationManager属性存储为强引用?

  

在涉及该对象的所有任务完成之前,需要保留对位置管理器对象的强引用。    由于大多数位置管理器任务以异步方式运行,因此将位置管理器存储在本地变量中是不够的。

现在是ViewController.h文件

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController<CLLocationManagerDelegate>
{

}
@property (nonatomic , strong) CLLocationManager *locationManager;
- (IBAction)actionGetCurrentLocation:(id)sender;
@end

在ViewController.m

中实施以下步骤

第五步:

  

在开始更新位置之前,我们应该请求权限并实例化CLLocationManager。然后我们应该开始更新位置

- (IBAction)actionGetCurrentLocation:(id)sender
{
   locationManager = [[CLLocationManager alloc]init];
   locationManager.delegate = self;
   locationManager.desiredAccuracy = kCLLocationAccuracyBest;
   if([CLLocationManager locationServicesEnabled] == NO){
      NSLog(@"Your location service is not enabled, So go to Settings>Location Services");
    }
   else{
      NSLog(@"Your location service is enabled");
   }
   if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
   {
      [locationManager requestWhenInUseAuthorization];
   }
   [locationManager startUpdatingLocation];
}    

第六步:

  

添加CLLocationManager委托方法

ViewController.m

#import "ViewController.h"

@interface ViewController ()
{
   CLLocation *currentLocation;
}
@end
@implementation ViewController
@synthesize locationManager;

- (void)viewDidLoad
{
   [super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
   NSLog(@"didFailWithError: %@", error);   //@"Error" //@"Failed to Get Your Location"  //@"OK"
   UIAlertController *errorAlert = [UIAlertController alertControllerWithTitle:@"Error" message:@"Failed to Get Your Location" preferredStyle:UIAlertControllerStyleAlert];
   UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
    [self dismissViewControllerAnimated:YES completion:nil];
    }];
   [self presentViewController:errorAlert animated:YES completion:nil];
   [errorAlert addAction:actionOK];
}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    currentLocation = newLocation;
    if (currentLocation != nil)
    {
        NSLog(@"%@",[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
        NSLog(@"%@",[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
     }
  }

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    NSLog(@"didUpdateLocation are : %@", locations);
    currentLocation = [locations lastObject];
    if (currentLocation != nil) 
    {
       NSLog(@"The latitude value is - %@",[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
       NSLog(@"The longitude value is - %@",[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
     }
}

#pragma mark - UIButton Action
 - (IBAction)actionGetCurrentLocation:(id)sender
{
   locationManager = [[CLLocationManager alloc]init];
   locationManager.delegate = self;
   locationManager.desiredAccuracy = kCLLocationAccuracyBest;
   if([CLLocationManager locationServicesEnabled] == NO){
      NSLog(@"Your location service is not enabled, So go to Settings>Location Services");
    }
   else{
      NSLog(@"Your location service is enabled");
   }
   if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
   {
      [locationManager requestWhenInUseAuthorization];
   }
   [locationManager startUpdatingLocation];
}