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

相关推荐

  • vgchange

    文章目录vgchange补充说明语法选项参数实例 vgchange 修改卷组属性 补充说明 vgchange命令 用于修改卷组的属性,经常被用来设置卷组是处于活动状态或非活动状态。…

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

    文章目录sshd补充说明语法选项 sshd openssh软件套件中的服务器守护进程 补充说明 sshd命令 是openssh软件套件中的服务器守护进程。 语法 sshd(选项) …

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

    文章目录md5sum补充说明语法选项参数实例 md5sum 计算和校验文件报文摘要的工具程序 补充说明 md5sum命令 采用MD5报文摘要算法(128位)计算和检查文件的校验和。…

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

    文章目录lnstat补充说明语法选项 lnstat 显示Linux系统的网路状态 补充说明 lnstat命令 用来显示Linux系统的网路状态。 语法 lnstat(选项) 选项 …

    入门教程 2023年 12月 19日
  • arpd

    文章目录arpd补充说明语法选项参数实例 arpd 收集免费ARP信息 补充说明 arpd命令 是用来收集免费arp信息的一个守护进程,它将收集到的信息保存在磁盘上或者在需要时,提…

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

    文章目录col补充说明语法选项 col 过滤控制字符 补充说明 col命令 是一个标准输入文本过滤器,它从标注输入设备读取文本内容,并把内容显示到标注输出设备。在许多UNIX说明文…

    入门教程 2023年 12月 7日
  • gcc

    文章目录gcc补充说明语法选项参数实例 gcc 基于C/C++的编译器 补充说明 gcc命令 使用GNU推出的基于 C/C++ 的编译器,是开放源代码领域应用最广泛的编译器,具有功…

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

    文章目录tree补充说明语法选项参数实例 tree 树状图列出目录的内容 补充说明 tree命令 以树状图列出目录的内容。 语法 tree(选项)(参数) 选项 ——- 列…

    入门教程 2024年 3月 11日
  • Helm | Helm 仓库列表

    文章目录helm repo list可选项从父命令继承的命令请参阅 helm repo list 列举chart仓库 可选项 -h, –help help for list -o…

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

    文章目录quotacheck补充说明语法选项参数实例 quotacheck 检查磁盘的使用空间与限制 补充说明 quotacheck命令 通过扫描指定的文件系统,获取磁盘的使用情况…

    入门教程 2024年 3月 1日
Translate »