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

相关推荐

  • pfctl

    文章目录pfctl补充说明激活配置控制 pfctl PF防火墙的配置命令 补充说明 pfctl命令 是PF防火墙的配置命令,PF防火墙( 全称:Packet Filter )是UN…

    入门教程 2024年 3月 1日
  • Helm | Helm 展示所有

    文章目录helm show all简介可选项从父命令继承的命令请参阅 helm show all 显示chart的所有信息 简介 该命令检查chart(目录、文件或URL)并显示所…

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

    文章目录readelf补充说明ELF文件类型选项实例 readelf 用于显示elf格式文件的信息 补充说明 readelf命令 用来显示一个或者多个elf格式的目标文件的信息,可…

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

    文章目录getenforce补充说明语法例子 getenforce 显示当前SELinux的应用模式,是强制、执行还是停用 补充说明 grename命令 可以重命名卷组的名称。 语…

    入门教程 2023年 12月 14日
  • Helm | Helm 展示chart

    文章目录helm show chart简介可选项从父命令继承的命令请参阅 helm show chart 显示chart定义 简介 该命令检查chart(目录、文件或URL)并显示…

    入门教程 2023年 12月 14日
  • Helm | Helm 仓库索引

    文章目录helm repo index简介可选项从父命令继承的命令请参阅 helm repo index 基于包含打包chart的目录,生成索引文件 简介 读取当前目录,并根据找到…

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

    文章目录restorecon补充说明语法选项实例 restorecon 恢复文件的安全上下文 补充说明 restorecon命令 用来恢复SELinux文件属性即恢复文件的安全上下…

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

    文章目录help补充说明语法选项参数常见问题实例 help 该命令是bash内建命令,用于显示bash内建命令的帮助信息。 补充说明 help命令 help命令只能显示bash内建…

    入门教程 2023年 12月 15日
  • unlink

    文章目录unlink补充说明语法选项参数 unlink 系统调用函数unlink去删除指定的文件 补充说明 unlink命令 用于系统调用函数unlink去删除指定的文件。和rm命…

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

    文章目录sar补充说明语法选项参数实例 sar 系统运行状态统计工具 补充说明 sar命令 是Linux下系统运行状态统计工具,它将指定的操作系统状态计数器显示到标准输出设备。sa…

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