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

2024-05-08

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


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

第一步:

在.h文件中导入CoreLocation框架

#import <CoreLocation/CoreLocation.h>

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

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

NSLocationAlwaysUsageDescription string 无论你想写什么,都写在这里

隐私 - 位置使用描述字符串 无论您想写什么,都写在这里

请看下面的屏幕截图

第三步:

添加 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 委托方法

视图控制器.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];
} 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

  • 检查 Objective-C 块类型?

    这主要是出于好奇 我不太确定它的实际用途是什么 但就这样吧 由于块也是 Objective C 对象 是否可以检查它们的类型 也就是说 它是否响应isKindOfClass 消息以及如何使用该消息来处理块 我天真的以为事情大概是这样的 vo
  • 子类 PFObject 上的 PFUser 属性

    我使用以下类 动态属性以及 m 文件中的 load 和 parseClassName 方法 对 PFObject 进行了子类化 interface DAOpponents PFObject
  • 对象指针值作为字典的键

    我想使用对象的引用值作为字典的键 而不是对象值的副本 因此 我本质上想在字典中存储与另一个对象的特定实例关联的对象 并稍后检索该值 这可能吗 是不是完全违背了NSDictionary的理念 我可以看出我可能以错误的方式处理这个问题 因为字典
  • 为什么 Objective-C 方法名称的最后一部分必须带有参数(当有多个部分时)?

    在 Objective C 中 您不能声明最后一个组件不带参数的方法名称 例如 以下内容是非法的 void take id theMoney andRun void take id yourMedicine andDontComplain
  • 重叠的装载机圆

    我试图重现苹果为应用程序 活动 制作的重叠圆圈 见下图 如果您使用标准贝塞尔路径 起始 结束位置将仅在 0 到 2PI 之间产生影响 例如 如果您尝试填充 4PI 即使使用一些阴影 则无法模拟重叠加载 如何制作类似于苹果解决方案的东西来创建
  • 如何解决malloc_error_break?

    我在 iOS 3 0 模拟器上遇到此错误 但在 3 1 3 和 3 2 模拟器上没有遇到此错误 创建符号断点后malloc error break 我在日志中看到了这一点 Session started at 2010 02 13 19 1
  • 如何重新定位或移动 Google Maps SDK 上的当前位置按钮?

    如何将 Objective C 中的当前位置按钮移至我的偏好 现在 我已启用它 但底角有东西挡住了它 Thanks 您可以使用 padding 将按钮向上移动 self mapView padding UIEdgeInsets top 0
  • 如何用图片替换UITableView?

    我有一个 UITableView 默认为空白 直到用户编辑并向其添加数据 我想显示一张带有说明的图像 直到用户编辑它为止 图片的大小非常适合导航栏和标签栏之间 有没有办法以编程方式执行此操作 您可以使用removeFromSuperview
  • Objective-C:int值无故改变

    Objective C 我需要帮助保留 int 的值 无需我的命令 它就在我身上发生变化 最初的问题是 如何声明和保留 int 这在另一篇文章中得到了满足 Objective C 如何声明和保留 int https stackoverflo
  • 如何从 AFNetworking 和 AFJSONRequestOperation 获取可变字典?

    我将 JSONKit 与 AFNetworking 的 AFHTTPClient 带有 AFJSONRequestOperation 一起使用 我似乎无法弄清楚如何触发使用 JSONKit 的 mutableObjectFrom 方法 而不
  • 确定 Objective-C 方法在运行时是否是可变的

    有没有办法在运行时找出给定方法是否是可变参数类型 就像是method getTypeEncoding 这不会告诉我一个方法是否接受可变数量的参数 或者有什么技巧可以告诉我们吗 罗伯特的评论是正确的 考虑 interface Boogity
  • 推入 UINavigationController 时隐藏 FBFriendPickerViewController 导航栏

    介绍一个实例FBFriendPickerViewController using presentViewController animated completion 非常简单 该类似乎是针对该用例的 但是 我想推送一个实例FBFriendP
  • iOS 中的 CSV 逐行解析

    我正在 Objective c 中解析 CSV 文件 该文件包含如下内容 line 40 Rising searches line 41 nabi avc Breakout line 42 stonewall 700 line 43 med
  • 来自 iPhone/iPad 的 json Web 服务

    有人可以帮助我解决如何从 iphone 或 ipad 使用 json Web 服务的问题吗 这里我的要求是使用 API 密钥实现 json webservice 如果可能的话发布一些教程或示例链接 谢谢 规范的 JSON 处理库是here
  • 在 UIImage 顶部绘制透明圆圈 - iPhone SDK

    我在尝试找出如何在 UIImageView 中的 UIImage 顶部绘制透明圆圈时遇到了很多麻烦 谷歌给了我线索 但我仍然找不到有效的例子 有没有人知道的例子可以证明这一点 最简单的方法就是创建一个半透明的方形 UIView 然后将其图层
  • 拖动时获取MKAnnotation的坐标

    我正在根据用户添加的注释的位置创建一条路径 MKPolyline 我想允许用户通过拖动引脚来更改路径 我目前可以做到这一点 但 MKPolyline 不会更新 直到引脚被放下 我实施了 void mapView MKMapView mapV
  • Objective-C中如何使继承的类能够看到父类的隐藏方法[重复]

    这个问题在这里已经有答案了 我有两个类 Class1 和 Class2 第二个类继承自第一个类 我需要重写 Class1 的 update 方法来实现我的目标 继承方法中 update方法的改变是在代码中间进行的 所以我不能使用 超级更新
  • 如何使用MKMapView完成加载委托,可能的“完成显示”委托?

    当用户在选择注释后点击 保存 时 我尝试保存地图视图的缩略图 当用户尚未放大该注释时会出现问题 因此尚未加载关闭缩放级别 这就是用户点击保存后我正在做的事情 将布尔值 saving 设置为 true 居中并放大注释 无动画 当调用mapVi
  • 如何在 UICollectionView 的节标题中动态添加标签和按钮?

    请帮助我如何水平添加标签和水平添加类似的按钮 但每个按钮应像另一个部分一样在每个标签的下方对齐 这应该在 UICollectionView 的标题中动态发生 因为标签和按钮的数量根据我的数据 我想制作一种 Excel 类型的布局 并在标题中
  • Objective-C / C 给出枚举默认值

    我在某处读到过关于给枚举默认值的内容 如下所示 typedef enum MarketNavigationTypeNone 0 MarketNavigationTypeHeirachy 1 MarketNavigationTypeMarke

随机推荐