【问题标题】:How to get user's current location from network provider in objective c?如何在目标 c 中从网络提供商获取用户的当前位置?
【发布时间】:2016-08-17 19:13:56
【问题描述】:

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

【问题讨论】:

标签: objective-c


【解决方案1】:

我给你完整的解决方案。如果你想实现这一点,你应该使用核心定位框架

第一步:

在 .h 文件中导入 CoreLocation 框架

#import <CoreLocation/CoreLocation.h>

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

NSLocationWhenInUseUsageDescription string 你想写什么就写在这里

NSLocationAlwaysUsageDescription string 你想写什么就写在这里

privacy - 位置使用描述字符串你想写什么就写在这里

请看下面的屏幕截图

第三步:

添加 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];
} 

【讨论】:

    猜你喜欢
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    相关资源
    最近更新 更多