IOS入门笔记之地理位置定位系统

2025-05-29 0 80

前言:关于地理位置定位系统,在iOS开发中也比较常见,比如美团外面的餐饮店铺的搜索,它首先需要用户当前手机的位置,然后在这个位置附近搜索相关的餐饮店铺的位置,并提供相关的餐饮信息,再比如最常见的就是地图导航,地图导航更需要定位服务,然后根据用户的目的地选出一条路线。其实,作为手机用户这么长时间,或多或少会发现在有些app应用首次在你的手机安装成功后,首次启动可能就会提示"是否同意XXx(比如百度浏览器)获取当前位置"等这样一类的信息。可见地理位置定位系统是企业app开发必不可少的技能。

本章将提供Swift版本和Objective-C两个版本的入门代码,分别实现显示当前手机或者是模拟器的地理经纬度坐标。

写在正式学习前的小贴士:

这是因为xcode升级造成的定位权限设置问题。
升级xcode6、xcode7以后打开以前xcode5工程,程序不能定位。工程升级到xcode6或xcode7编译时需要iOS8 要自己写授权,不然没权限定位。

解决方法:

首先在 info.plist里加入对应的缺省字段 ,值设置为YES(前台定位写上边字段,前后台定位写下边字段)
NSLocationWhenInUseUsageDescription //允许在前台获取GPS的描述
NSLocationAlwaysUsageDescription //允许在前、后台获取GPS的描述

设置的图示:

好了,如果设置好了,那就正式进入编码学习吧,首先熟悉苹果提供的关于定位服务相关的类,方法以及属性:

1、定位服务和地图应用的介绍

定位服务: 获取用户当前的位置信息,针对用户的位置信息做相关的数据处理。

地图应用: 根据实际需求展示地图和周边环境信息,基于用户当前位置展示用户所关注的地图位置信息、以及为用户导航。

•定位服务要掌握的:

•主要操作的类:CLLocationManager

•所属库:CoreLocation

•结构体:CLLocationCoordinate2D(经纬度)、CLCLocationCoorRegion(区域)

•地图应用需要掌握的:

•框架:MapKit

•操作类:MKMapView

2、定位服务

•属性:

•desiredAccuracy设置定位精确度,这是一个常量属性,一般用best
•distanceFilter 重新定位的最小变化距离

方法:

•设置什么时候开启定位的状态 •requestAlwaysAuthorization() 始终开启定位
•requestWhenInUseAuthorization() 当app进入前台的时候开启定位(iOS8的新方法)
•类方法locationServicesEnabled() 是否有定位服务功能(CLLocationManager)
•startUpdatingLocation() 开启定位

代理:

•代理的协议:
•代理的方法:可以直接进入这个库的API查看,只要就是定位错误调用的代理方法,定位成功调用的代理方法等等;

涉及到的对象

•locations: CLLocation 该CLLocation对象的属性: •coordinate •longitude/latitude

英语词汇积累:

•accuracy 英 'ækjʊrəsɪ n. [数] 精确度,准确性
•filter 英 'fɪltə 滤波器 过滤器;筛选;滤光器 过滤;渗透;用过滤法除去

下面提供的是Swift源码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52
//

// ViewController.swift

// LocationManager

//

// Created by HEYANG on //.

// Copyright © 年 HEYANG. All rights reserved.

//

import UIKit

// 需要导入CoreLocation框架

import CoreLocation

class ViewController: UIViewController,CLLocationManagerDelegate {

// 声明一个全局变量

var locationManager:CLLocationManager!

override func viewDidLoad() {

super.viewDidLoad()

locationManager = CLLocationManager()

// 设置定位的精确度

locationManager.desiredAccuracy = kCLLocationAccuracyBest

// 设置定位变化的最小距离 距离过滤器

locationManager.distanceFilter =

// 设置请求定位的状态

if #available(iOS ., *) {

locationManager.requestWhenInUseAuthorization()

} else {

// Fallback on earlier versions

print("hello")

}//这个是在ios之后才有的

// 设置代理为当前对象

locationManager.delegate = self;

if CLLocationManager.locationServicesEnabled(){

// 开启定位服务

locationManager.startUpdatingLocation()

}else{

print("没有定位服务")

}

}

// 定位失败调用的代理方法

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {

print(error)

}

// 定位更新地理信息调用的代理方法

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

if locations.count >

{

let locationInfo = locations.last!

let alert:UIAlertView = UIAlertView(title: "获取的地理坐标",

message: "经度是:\\(locationInfo.coordinate.longitude),维度是:\\(locationInfo.coordinate.latitude)",

delegate: nil, cancelButtonTitle: "是的")

alert.show()

}

}

}

下面是Objective-C的源码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46
//

// ViewController.m

// LocationManager

//

// Created by HEYANG on //.

// Copyright © 年 HEYANG. All rights reserved.

//

#import "ViewController.h"

#import <CoreLocation/CoreLocation.h>

@interface ViewController () <CLLocationManagerDelegate>

/** 全局定位对象 */

@property (nonatomic,strong)CLLocationManager *locationManager;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

CLLocationManager* locationManager = [[CLLocationManager alloc] init];

// 设置定位精确度

locationManager.desiredAccuracy = kCLLocationAccuracyBest;

// 设置定位变化最小距离

locationManager.distanceFilter = ;

// 设置定位服务的使用状态

[locationManager requestWhenInUseAuthorization];

locationManager.delegate = self;

if ([CLLocationManager locationServicesEnabled]) {

[locationManager startUpdatingLocation];

}else{

NSLog(@"本机不支持定位服务功能");

}

self.locationManager = locationManager;

}

// 定位失败调用的代理方法

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

NSLog(@"错误信息:%@",error);

}

// 定位数据更新调用的代理方法

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

if (locations.count > ) {

CLLocation* location = locations.lastObject;

CLLocationCoordinateD coordinateD = location.coordinate;

NSString* message = [NSString stringWithFormat:@"经度:%lf,维度是:%lf",coordinateD.longitude,coordinateD.latitude];

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"显示当前位置的经纬度"                 message:message delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

[alertView show];

}

}

@end

以上是小编给大家分享的IOS入门笔记之地理位置定位系统,希望对大家有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 IOS入门笔记之地理位置定位系统 https://www.kuaiidc.com/94061.html

相关文章

猜你喜欢
发表评论
暂无评论