标签:
//懒加载位置管理者
private lazy var location : CLLocationManager = {
//创建位子管理者
let location = CLLocationManager()
//设置代理
location.delegate = self
//返回位置管理者
return location
}()
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//开始定位
location.startUpdatingLocation()
}
extension ViewController : CLLocationManagerDelegate {
//当获取到用户的位置信息的时候会调用这个方法
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("定位到了")
//如果只想获取一次用户位置,可以在获取到位置信息之后,停止获取
manager.stopUpdatingLocation()
}
}
//懒加载
private lazy var location : CLLocationManager = {
//创建位置管理者
let location = CLLocationManager()
//设置代理
location.delegate = self
location.requestWhenInUseAuthorization()
//返回
return location
}()
extension ViewController : CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("定位到了")
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//点击调用
location.startUpdatingLocation()
}
class ViewController: UIViewController {
private lazy var location : CLLocationManager = {
let location = CLLocationManager()
location.delegate = self
if #available(iOS 8.0, *){
location.requestWhenInUseAuthorization()
}
if #available(iOS 9.0, *){
location.allowsBackgroundLocationUpdates = true
}
return location
}()
extension ViewController : CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("定位到了")
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
location.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
//判断用户决定代码
}
NotDetermined // 用户未决定
Restricted // 受限制
Denied // 用户拒绝
AuthorizedAlways // 前后台定位授权
AuthorizedWhenInUse // 前台定位授权
//switch判断
switch status {
case .NotDetermined :
print("用户未决定")
case .Restricted :
print("受限制")
case .Denied :
if CLLocationManager.locationServicesEnabled() {
print("用户正在拒绝")
//如果定位服务开启,用户拒绝了,系统不会自动弹框,需要手动给用户提示
if #available(iOS 8.0, *) {
let url = NSURL(string: UIApplicationOpenSettingsURLString)!
if UIApplication.sharedApplication().canOpenURL(url){
UIApplication.sharedApplication().openURL(url)
}
}else {
//准备多张图片,用来引导用户去设置界面允许开启定位服务
}
} else {
print("定位服务关闭,建议打开定位服务")
//如果定位服务关闭,用户下次再打开时候,系统会自动弹框,让用户去设置界面打开定位服务
}
case .AuthorizedAlways :
print("前后台定位授权")
case .AuthorizedWhenInUse :
print("前台定位授权")
}
}
//该方法定位失败的时候就会调用error失败信息 CLLocationManager位置管理者
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
}
/*
1 设置每个多少米定位一次?
1.1 一次
1.2 111km / 100次
2 当距离大于100米的时候回调用一次,获取用户的位置信息
*/
location.distanceFilter = 100
/*
1 定位精确度高,定位越准确,越耗电
2 一般情况下,在能够满足条件情况下,使用精确度比较低的
3 一旦定位到用户的位置之后,如果不需要用户的位置了,建议关闭获取用户位置
*/
location.desiredAccuracy = kCLLocationAccuracyBest
if #available(iOS 9.0, *) {
location.requestLocation()
}
func distanceFromLocation(location: CLLocation) -> CLLocationDistance
if location.horizontalAccuracy < 0 {return}
//懒加载
private lazy var location :CLLocationManager = {
//创建位置管理者
var location = CLLocationManager()
//设置代理
location.delegate = self
if #available(iOS 8.0, *) {location.requestAlwaysAuthorization()}
location.desiredAccuracy = kCLLocationAccuracyBest
return location
}()
//定义一个属性用来记录用户的上一个位置
private var lastLocation : CLLocation?
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//在位置数组中,最后一个是最新的
//取出位置
guard let location = locations.last else{return}
//判断位置是否可用
if location.horizontalAccuracy < 0 {return}
//1.确定航向
if location.course < 0 {return}
let angleStrs = ["北偏东","东偏南","南偏西","西偏北"]
//当前偏移方向
let index = Int(location.course / 90)
//取出数组中的方向
var angleStr = angleStrs[index]
//2.确定偏移角度
let angle = location.course % 90
//判断角度
if Int(index) == 0 {
let index = angleStr.startIndex.advancedBy(1)
angleStr = "正" + angleStr.substringToIndex(index)
}
//3.确定移动的距离
let lastLoc = lastLocation ?? location
//计算出两个点之间的物理距离
let distance = location.distanceFromLocation(lastLoc)
//赋值
lastLocation = lastLoc
//4.拼接字符串,打印距离
if Int(index) == 0 {
print("\(angleStr),移动了\(distance)米")
} else {
print("\(angleStr) \(angle)度,移动了\(distance)米")
}
print("定位到了")
}
//指南针
@IBOutlet weak var compassImageView: UIImageView!
import CoreLocation
//懒加载
private lazy var location : CLLocationManager = {
//创建位置管理者
var location = CLLocationManager()
//设置代理
location.delegate = self
//返回位置管理者
return location
}()
//代理方法的实现
extension ViewController : CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
//判断朝向是否可用
if newHeading.headingAccuracy < 0 {return}
//获取朝向设备和角度
let magneticHeading = newHeading.magneticHeading
//将角度转化为弧度
let radian = CGFloat(magneticHeading / 180 * M_PI)
//反转图片弧度
UIView.animateWithDuration(0.25) { () -> Void in
self.compassImageView.transform = CGAffineTransformMakeRotation(-radian)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
//判断设备是否可以用
if CLLocationManager.headingAvailable() { //可用
location.startUpdatingHeading()
} else { //不可用
SVProgressHUD.showErrorWithStatus("指南针不可用,请更换手机")
}
}
@IBOutlet weak var noticeLabel: UILabel!
private lazy var location : CLLocationManager = {
//创建位置管理者
let location = CLLocationManager()
//设置代理
location.delegate = self
//允许前后台授权
location.requestAlwaysAuthorization()
//返回位置管理者
return location
}()
override func viewDidLoad() {
super.viewDidLoad()
//判断该区域是否可以被监听
if CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion) {
//创建区域
let center = CLLocationCoordinate2DMake(21.123, 123.456)
//设置半径
var radius : CLLocationDistance = 10.0
//标识
let identify = "肖锋区域"
//检测是否超过了最大的监听范围
if radius > location.maximumRegionMonitoringDistance {
//如果超过了范围,就让他等于最大值
radius = location.maximumRegionMonitoringDistance
}
let region = CLCircularRegion(center: center, radius: radius, identifier: identify)
//监听区域
// location.startMonitoringForRegion(region)
location.requestStateForRegion(region)
}
}
extension ViewController : CLLocationManagerDelegate {
//离开区域
func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
print("您已经离开该区域")
}
//进入区域
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("您已经进入该区域")
}
//区域状态发生改变
func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion) {
//判断状态
switch state {
case .Unknown:
noticeLabel.text = "不知道"
case .Inside:
noticeLabel.text = "进入"
case .Outside:
noticeLabel.text = "离开"
}
}
//输入的view
@IBOutlet weak var addressTV: UITextView!
//经度
@IBOutlet weak var longitudeTF: UITextField!
//纬度
@IBOutlet weak var latitudeTF: UITextField!
//懒加载
private lazy var geoc : CLGeocoder = {
return CLGeocoder()
}()
//地理编码
@IBAction func geocodingBtnClick()
{
let address = addressTV.text
geoc.geocodeAddressString(address) { (clplas :[CLPlacemark]?,error : NSError?) -> Void in
if error == nil {
//对CLPlacemark的解析
//1. 获取地标对象,取第一个,相关度最高
guard let clplas = clplas else {return}
guard let clpl = clplas.first else {return}
//2. 设置地址
self.addressTV.text = clpl.name!
//设置经度和纬度 (coordinate:当前位置)
self.longitudeTF.text = "\(clpl.location!.coordinate.longitude)"
self.latitudeTF.text = "\(clpl.location!.coordinate.latitude)"
}
}
}
//反地理编码
@IBAction func reverGeocodingBtnClick()
{
//获取位置的经度和纬度数据
let latitude = CLLocationDegrees(self.latitudeTF.text!)
let longitude = CLLocationDegrees(self.longitudeTF.text!)
let location = CLLocation(latitude: latitude!, longitude: longitude!)
geoc.reverseGeocodeLocation(location) { (clplas : [CLPlacemark]?,error : NSError?) -> Void in
if error == nil {
//1. 获取地标对象,取第一个,相关度最高
guard let clplas = clplas else {return}
guard let clpl = clplas.first else {return}
//2. 设置地址
self.addressTV.text = clpl.name!
//设置经度和纬度 (coordinate:当前位置)
self.longitudeTF.text = "\(clpl.location!.coordinate.longitude)"
self.latitudeTF.text = "\(clpl.location!.coordinate.latitude)"
}
}
}
import UIKit
import CoreLocation
class ViewController: UIViewController {
//懒加载(一)
private lazy var location : CLLocationManager = {
//创建位置管理者
let location = CLLocationManager()
//设置代理
location.delegate = self
//始终获取用户位置
location.requestAlwaysAuthorization()
//返回
return location
}()
//懒加载(二)
private lazy var geco : CLGeocoder = {
return CLGeocoder()
}()
//当点击屏幕的时候开始获取用户的位置
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
location.startUpdatingLocation()
}
}
//实现代理方法
extension ViewController : CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//获取用户位置是否可用
guard let location = locations.last else {return}
//判断位置是否可用
if location.horizontalAccuracy < 0 {return}
//获取经度和纬度进行反地理编码
geco.reverseGeocodeLocation(location) {(clplas : [CLPlacemark]?,error : NSError?) -> Void in
//判断错误信息
if error == nil {
guard let clplas = clplas else {return}
guard let clpl : CLPlacemark = clplas.first else {return}
//打印地址
print(clpl.name!)
}
}
}
}
let locationMan = INTULocationManager.sharedInstance()
// 获取一次用户信息
let locationRequestID : INTULocationRequestID = locationMan.requestLocationWithDesiredAccuracy(.Room, timeout: 5, delayUntilAuthorized: false) { (location : CLLocation!, _,status : INTULocationStatus) -> Void in
//判断
if status == INTULocationStatus.Success {
print("获取到位置")
} else {
print(status.rawValue)
}
}
let LocationRequestID: INTULocationRequestID = locationMan.subscribeToLocationUpdatesWithDesiredAccuracy(.Room) { (location : CLLocation!, _,status : INTULocationStatus) -> Void in
if status == INTULocationStatus.Success {
print("获取到位置")
} else {
print(status.rawValue)
}
}
标签:
原文地址:http://blog.csdn.net/xf931456371/article/details/51341091