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

相关推荐

  • indent

    文章目录indent补充说明语法选项实例 indent 格式化C语言的源文件 补充说明 indent命令 可辨识C的原始代码文件,并加以格式化,以方便程序员阅读、修改等操作。 语法…

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

    文章目录lp补充说明语法选项参数实例 lp 打印文件或修改排队的打印任务 补充说明 lp命令 用于打印文件,或者修改排队的打印任务。与lpr命令类似,lp命令既支持文件输入也支持标…

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

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

    入门教程 2024年 3月 1日
  • Linux统计文件夹下的文件数目

    Linux下有三个命令:ls、grep、wc。通过这三个命令的组合可以统计目录下文件及文件夹的个数。 统计当前目录下文件的个数(不包括目录) ls -l | grep "…

    入门教程 2024年 4月 18日
  • rcp

    文章目录rcp补充说明语法选项参数实例 rcp 使在两台Linux主机之间的文件复制操作更简单 补充说明 rcp命令 使在两台Linux主机之间的文件复制操作更简单。通过适当的配置…

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

    文章目录rpmverify补充说明语法选项 rpmverify 验证已安装的RPM软件包的正确性 补充说明 rpmverify命令 用来验证已安装的rpm软件包的正确性。 语法 r…

    入门教程 2024年 3月 4日
  • lynx

    文章目录lynx补充说明语法选项参数内部命令 lynx 纯文本模式的网页浏览器 补充说明 lynx命令 是纯文本模式的网页浏览器,不支持图形、音视频等多媒体信息。 语法 lynx(…

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

    文章目录pstack补充说明实例 pstack 显示每个进程的栈跟踪 补充说明 pstack命令 可显示每个进程的栈跟踪。pstack 命令必须由相应进程的属主或 root 运行。…

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

    文章目录ifstat补充说明下载编译安装选项实例 ifstat 统计网络接口流量状态 补充说明 ifstat命令 就像iostat/vmstat描述其它的系统状况一样,是一个统计网…

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

    文章目录zipinfo补充说明语法选项参数 zipinfo 用来列出压缩文件信息 补充说明 zipinfo命令 用来列出压缩文件信息。执行zipinfo指令可得知zip压缩文件的详…

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