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

相关推荐

  • gzexe

    文章目录gzexe补充说明语法选项参数 gzexe 用来压缩可执行文件 补充说明 gzexe命令 用来压缩可执行文件,压缩后的文件仍然为可执行文件,在执行时进行自动解压缩。当您去执…

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

    文章目录lsof补充说明语法选项实例 lsof 显示Linux系统当前已打开的所有文件列表 lsof -p pid 补充说明 lsof命令 用于查看你进程打开的文件,打开文件的进程…

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

    文章目录lvreduce补充说明语法选项参数实例 lvreduce 收缩逻辑卷空间 补充说明 lvreduce命令 用于减少LVM逻辑卷占用的空间大小。使用lvreduce命令收缩…

    入门教程 2023年 12月 19日
  • HTTP 响应头信息

    HTTP请求头提供了关于请求,响应或者其他的发送实体的信息。 在本章节中我们将具体来介绍HTTP响应头信息。 应答头 说明 Allow 服务器支持哪些请求方法(如GET、POST等…

    2023年 5月 14日
  • whereis

    文章目录whereis补充说明语法选项参数实例 whereis 查找二进制程序、代码等相关文件路径 补充说明 whereis命令 用来定位指令的二进制程序、源代码文件和man手册页…

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

    文章目录pvremove补充说明语法选项参数实例 pvremove 删除一个存在的物理卷 补充说明 pvremove命令 用于删除一个存在的物理卷。使用pvremove指令删除物理…

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

    文章目录diff补充说明语法选项参数实例以正常模式比较差异以上下文 (context) 模式比较差异以联合 (unified) 模式比较差异多文件比较差异 diff 比较给定的两个…

    入门教程 2023年 12月 7日
  • Java 抽象类

    在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。 抽象类除了…

    入门教程 2023年 3月 9日
  • csplit

    文章目录csplit补充说明语法选项参数实例 csplit 将一个大文件分割成小的碎片文件 补充说明 csplit命令 用于将一个大文件分割成小的碎片,并且将分割后的每个碎片保存成…

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

    文章目录uname概要主要用途选项返回值例子注意 uname 打印系统信息。 概要 uname [OPTION]… 主要用途 打印机器和操作系统的信息。 当没有选项时,默认启用…

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