iOS定位操作

IOS定位操作


简介

在IOS中通过CoreLocation定位,可以获取到用户当前位置,同时能得到装置移动信息。

实例步骤

1、创建一个简单的View based application(视图应用程序)。

2、择项目文件,然后选择目标,然后添加CoreLocation.framework,如下所示

CoreLocation_Library_Addition

3、在ViewController.xib中添加两个标签,创建ibOutlet名为latitudeLabel和longtitudeLabel的标签

4、现在通过选择 File-> New -> File… -> 选择Objective C class 并单击下一步

5、把sub class of作为NSObject,将类命名为LocationHandler

6、选择创建

7、更新LocationHandler.h,如下所示

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

@protocol LocationHandlerDelegate <NSObject>

@required
-(void) didUpdateToLocation:(CLLocation*)newLocation 
 fromLocation:(CLLocation*)oldLocation;
@end

@interface LocationHandler : NSObject<CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
}
@property(nonatomic,strong) id<LocationHandlerDelegate> delegate;

+(id)getSharedInstance;
-(void)startUpdating;
-(void) stopUpdating;

@end

8、更新LocationHandler.m,如下所示

#import "LocationHandler.h"
static LocationHandler *DefaultManager = nil;

@interface LocationHandler()

-(void)initiate;

@end

@implementation LocationHandler

+(id)getSharedInstance{
    if (!DefaultManager) {
        DefaultManager = [[self allocWithZone:NULL]init];
        [DefaultManager initiate];
    }
    return DefaultManager;
}
-(void)initiate{
    locationManager = [[CLLocationManager alloc]init];
    locationManager.delegate = self;
}

-(void)startUpdating{
    [locationManager startUpdatingLocation];
}

-(void) stopUpdating{
    [locationManager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:
 (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    if ([self.delegate respondsToSelector:@selector
    (didUpdateToLocation:fromLocation:)]) 
    {
        [self.delegate didUpdateToLocation:oldLocation 
        fromLocation:newLocation];

    }
}

@end

9、更新ViewController.h,如下所示

#import <UIKit/UIKit.h>
#import "LocationHandler.h"
@interface ViewController : UIViewController<LocationHandlerDelegate>
{
    IBOutlet UILabel *latitudeLabel;
    IBOutlet UILabel *longitudeLabel;
}
@end

10、更新ViewController.m,如下所示

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[LocationHandler getSharedInstance]setDelegate:self];
    [[LocationHandler getSharedInstance]startUpdating];
}

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

-(void)didUpdateToLocation:(CLLocation *)newLocation 
 fromLocation:(CLLocation *)oldLocation{
    [latitudeLabel setText:[NSString stringWithFormat:
    @"Latitude: %f",newLocation.coordinate.latitude]];
    [longitudeLabel setText:[NSString stringWithFormat:
    @"Longitude: %f",newLocation.coordinate.longitude]];

}

@end

输出

当我们运行该应用程序,会得到如下的输出:

locationOutput

若文章对你有帮助,可以点赞或打赏支持我们。发布者:lyh会员,转载请注明出处:http://61.174.243.28:13541/AY-knowledg-hub/ios%e5%ae%9a%e4%bd%8d%e6%93%8d%e4%bd%9c-%e8%8f%9c%e9%b8%9f%e6%95%99%e7%a8%8b/

(0)
lyhlyh会员认证作者
上一篇 2023年 3月 30日 下午9:48
下一篇 2023年 4月 1日 下午8:22

相关推荐

  • 常用函数

    以下列出了 Pandas 常用的一些函数及使用实例: 文章目录读取数据实例查看数据实例实例数据清洗实例数据选择和切片实例数据排序实例数据分组和聚合实例数据合并实例数据选择和过滤数据…

    2023年 5月 16日
  • time

    文章目录time补充说明语法参数实例软件包软件包的语法软件包指令参数软件包实例参考资料 time 统计给定命令所花费的总时间 补充说明 time 命令是用来确定一个给定的命令需要运…

    入门教程 2024年 3月 11日
  • expand

    文章目录expand补充说明语法选项参数 expand 将文件的制表符转换为空白字符 补充说明 expand命令 用于将文件的制表符(TAB)转换为空白字符(space),将结果显…

    入门教程 2023年 12月 14日
  • xclip

    xclip 管理 X 粘贴板 补充说明 在 X 系统里面,从一个窗口复制一段文字到另一个窗口,有两套机制,分别是 Selections 和 cut buffers。 常用的 cop…

    入门教程 2024年 3月 11日
  • iOS SQLite数据库

    文章目录简介实例步骤输出 简介 在IOS中使用Sqlite来处理数据。如果你已经了解了SQL,那你可以很容易的掌握SQLite数据库的操作。 实例步骤 1、创建一个简单的View …

    2023年 4月 1日
  • repquota

    文章目录repquota补充说明语法选项参数实例 repquota 报表的格式输出磁盘空间限制的状态 补充说明 repquota命令 以报表的格式输出指定分区,或者文件系统的磁盘配…

    入门教程 2024年 3月 1日
  • tput

    文章目录tput补充说明什么是 terminfo 数据库?实例 tput 通过terminfo数据库对终端会话进行初始化和操作 补充说明 tput命令 将通过 terminfo 数…

    入门教程 2024年 3月 11日
  • getenforce

    文章目录getenforce补充说明语法例子 getenforce 显示当前SELinux的应用模式,是强制、执行还是停用 补充说明 grename命令 可以重命名卷组的名称。 语…

    入门教程 2023年 12月 14日
  • nologin

    文章目录nologin补充说明语法实例 nologin 拒绝用户登录系统 补充说明 nologin命令 可以实现礼貌地拒绝用户登录系统,同时给出信息。如果尝试以这类用户登录,就在l…

    入门教程 2024年 1月 10日
  • grub

    文章目录grub补充说明语法选项实例参考资料 grub 多重引导程序grub的命令行shell工具 补充说明 grub命令 是多重引导程序grub的命令行shell工具。 语法 g…

    入门教程 2023年 12月 14日
Translate »