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

相关推荐

  • htdigest

    文章目录htdigest补充说明语法选项参数 htdigest Apache服务器内置工具 补充说明 htdigest命令 是Apache的Web服务器内置工具,用于创建和更新储存…

    入门教程 2023年 12月 15日
  • 介绍和安装

    文章目录介绍和安装什么是Golang为什么选择Golang选择Go时的一些优点安装苹果系统Windows 系统Linux 系统环境配置linux1.下载安装包 or 直接安装2.解…

    2023年 12月 5日
  • ftptop

    文章目录ftptop补充说明语法选项 ftptop proftpd服务器的连接状态 补充说明 ftptop命令 类似于top命令的显示风格显示proftpd服务器的连接状态。 语法…

    入门教程 2023年 12月 14日
  • dpkg-trigger

    文章目录dpkg-trigger补充说明语法选项参数 dpkg-trigger Debian Linux下的软件包触发器 补充说明 dpkg-trigger命令 是Debian L…

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

    文章目录parted补充说明语法选项参数实例 parted 磁盘分区和分区大小调整工具 补充说明 parted命令 是由GNU组织开发的一款功能强大的磁盘分区和分区大小调整工具,与…

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

    文章目录nisdomainname补充说明语法选项 nisdomainname 显示主机NIS的域名 补充说明 nisdomainname命令 用于显示主机NIS的域名。 语法 n…

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

    文章目录jwhois补充说明语法选项实例 jwhois whois 客户端服务 补充说明 jwhois searches Whois servers for the object …

    入门教程 2023年 12月 19日
  • Helm | Helm 仓库

    文章目录helm repo简介可选项从父命令继承的命令请参阅 helm repo 添加、列出、删除、更新和索引chart仓库 简介 该命令由于chart仓库交互的多条子命令组成 可…

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

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

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

    文章目录reposync概要主要用途选项例子文件 reposync 同步yum存储库到本地目录 概要 reposync [选项] 主要用途 reposync用于将远程yum存储库同…

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