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

相关推荐

  • gzip

    文章目录gzip补充说明语法选项参数实例 gzip 用来压缩文件 补充说明 gzip命令 用来压缩文件。gzip是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多处“.gz”…

    入门教程 2023年 12月 14日
  • CentOS安装gcc组件

    CentOS不带gcc及组件,要使用就需要安装。 一键安装 yum -y install gcc gcc-c++ kernel-devel //安装gcc、c++编译器以及内核文件

    2021年 7月 13日
  • bind

    文章目录bind补充说明语法选项实例 bind 显示或设置键盘按键与其相关的功能 补充说明 bind命令 用于显示和设置命令行的键盘序列绑定功能。通过这一命令,可以提高命令行中操作…

    入门教程 2023年 12月 6日
  • unrar

    文章目录unrar语法安装选项SWITCHES 开关设置参数实例 unrar 解压rar文件命令,从 rar 压缩包中提取文件 语法 unrar [选项][switch 命令] […

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

    文章目录arj补充说明语法参数 arj 用于创建和管理.arj压缩包 补充说明 arj命令 是 .arj 格式的压缩文件的管理器,用于创建和管理 .arj 压缩包。 语法 arj(…

    入门教程 2023年 12月 6日
  • HTML 编辑器

    可以使用专业的 HTML 编辑器来编辑 HTML,菜鸟教程为大家推荐几款常用的编辑器: VS Code:https://code.visualstudio.com/ Sublime…

    2023年 4月 11日
  • Java 多态

    多态是同一个行为具有多个不同表现形式或形态的能力。 多态就是同一个接口,使用不同的实例而执行不同操作,如图所示: 多态性是对象多种表现形式的体现。 现实中,比如我们按下 F1 键这…

    2023年 3月 9日
  • squidclient

    文章目录squidclient补充说明语法选项参数 squidclient squid服务器的客户端管理工具 补充说明 squidclient命令 使用squid服务器的客户端管理…

    入门教程 2024年 3月 5日
  • systool

    文章目录systool补充说明语法选项参数实例 systool 显示基于总线、类和拓扑显示系统中设备的信息 补充说明 systool命令 指令显示基于总线、类和拓扑显示系统中设备的…

    入门教程 2024年 3月 11日
  • 缓冲信道和工作池(Buffered Channels and Worker Pools)

    文章目录缓冲信道和工作池(Buffered Channels and Worker Pools)什么是缓冲信道?示例一示例二死锁长度 vs 容量WaitGroup工作池的实现 缓冲…

    2023年 12月 5日
Translate »