首页 > 系统 > iOS > 正文

IOS开发中地理位置定位系统的操作方法

2020-02-19 15:53:30
字体:
来源:转载
供稿:网友

IOS开发中地理位置定位系统的操作方法大家了解吗?其实这个功能在IOS系统中是很常见的,今天武林技术频道小编就为大家简单介绍一下吧,希望对你了解这方面知识有帮助!

IOS开发中地理位置定位系统的操作方法

这是因为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源码:

//// ViewController.swift// LocationManager//// Created by HEYANG on //.// Copyright © 年 HEYANG. All rights reserved.//import UIKit// 需要导入CoreLocation框架import CoreLocationclass 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 versionsprint(///"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的源码:

//// ViewController.m// LocationManager//// Created by HEYANG on //.// Copyright © 年 HEYANG. All rights reserved.//#import ///"ViewController.h///"#import @interface ViewController () /** 全局定位对象 */@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 *)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开发中地理位置定位系统的操作方法,希望对大家有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表