如何在目标c中从网络提供商获取用户的当前位置? [英] How to get user's current location from network provider in objective c?
问题描述
在离线模式下,如何在ios中获取用户的当前位置,是否可以通过网络提供商获取位置?
In offline mode how to get user's current location in ios, is it possible to get the location by network provider?
推荐答案
我为您提供完整的解决方案.如果要实现此目的,则应使用核心位置框架.
I give you full solution.If you want to achieve this you should use core location framework.
第一步:
在.h文件中导入CoreLocation框架
import CoreLocation framework in .h file
#import <CoreLocation/CoreLocation.h>
第二步:您需要在plist文件中添加以下内容
Second Step: You need to add following things in your plist file
NSLocationWhenInUseUsageDescription字符串无论您想写什么,都写在这里
NSLocationWhenInUseUsageDescription string Whatever you want to write write here
NSLocationAlwaysUsageDescription字符串无论您想写什么,都写在这里
NSLocationAlwaysUsageDescription string Whatever you want to write write here
隐私-位置使用情况描述字符串无论您想写什么,都写在这里
privacy - location usage description string Whatever you want to write write here
请参见下面的屏幕截图
第三步:
添加CLLocationManagerDelegate
Add CLLocationManagerDelegate
第四步:
创建CLLocationManager类的实例并存储强引用
Create an instance of the CLLocationManager class and store a strong reference
@property (nonatomic , strong) CLLocationManager *locationManager;
为什么我们在这里将CLLocationManager属性存储为强引用?
在完成涉及该对象的所有任务之前,需要对位置管理器对象保持强烈的引用. 由于大多数位置管理器任务都是异步运行的,因此将位置管理器存储在本地变量中是不够的.
Keeping a strong reference to the location manager object is required until all tasks involving that object are complete. Because most location manager tasks run asynchronously, storing your location manager in a local variable is insufficient.
现在的ViewController.h文件
Now ViewController.h file
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
}
@property (nonatomic , strong) CLLocationManager *locationManager;
- (IBAction)actionGetCurrentLocation:(id)sender;
@end
要在ViewController.m中实现的以下步骤
Following steps to be implemented in ViewController.m
第五步:
在开始更新位置之前,我们应该征得许可并实例化CLLocationManager.然后,我们应该开始更新位置
Before starting to update the location we should ask permission and instantiate the CLLocationManager.Then we should start updating the location
- (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委托方法
Add CLLocationManager Delegate methods
ViewController.m
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];
}
这篇关于如何在目标c中从网络提供商获取用户的当前位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!