swift地图定位(九)github-LocationManager的使用

孙震博
2023-12-01

框架见https://github.com/intuit/LocationManager

由于是OC 版的,所以使用时要建立一个桥接文件才能在swift中使用

步骤:

1下载框架

2拖入/Users/targetcloud/Downloads/LocationManager-master/LocationManager/INTULocationManager文件夹到工程中

3建立一个头文件,写入一条导入语句 

#import "INTULocationManager.h"

4点Build Settings,查找框输入brid后,找到下面列表的 Objective-C Bridging Header 输入 1114-lm/Bridge.h(工程目录名/桥接文件名)

//
//  ViewController.swift
//  1114-lm
//
//  Created by targetcloud on 2016/11/14.
//  Copyright © 2016年 targetcloud. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    //2016-11-14 16:18:34.260 1114-lm[2594:53227] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'To use location services in iOS 8+, your Info.plist must provide a value for either NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription.'
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let locMgr = INTULocationManager.sharedInstance()
        //typedef void(^INTULocationRequestBlock)(CLLocation *currentLocation, INTULocationAccuracy achievedAccuracy, INTULocationStatus status);
        // delayUntilAuthorized 超时时间从什么时候开始计算
        // true , 从用户选择授权之后开始计算
        // false, 从执行这个代码开始计算
        let requestID = locMgr.requestLocation(withDesiredAccuracy: .room, timeout: 10, delayUntilAuthorized: true){ (currentLocation: CLLocation?, achievedAccuracy: INTULocationAccuracy, status: INTULocationStatus)  in
            if status == INTULocationStatus.success {
                print(currentLocation!)
                /*单次更新
                 2016-11-14 16:19:43.721 1114-lm[2668:54135] INTULocationManager: Location services updates have started.
                 2016-11-14 16:19:43.721 1114-lm[2668:54135] INTULocationManager: Location Request added with ID: 1
                 2016-11-14 16:19:46.008 1114-lm[2668:54135] INTULocationManager: Location services updates have stopped.
                 2016-11-14 16:19:46.018 1114-lm[2668:54135] INTULocationManager: Location Request completed with ID: 1, currentLocation: <+31.78583400,+121.40641700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 2016/11/14 中国标准时间 下午4:19:46, achievedAccuracy: 5, status: 0
                 <+31.78583400,+121.40641700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 2016/11/14 中国标准时间 下午4:19:46
                */
            }else {
                print(status.rawValue)
            }
        }
        
        // Force the request to complete early, like a manual timeout (will execute the block)
//        locMgr.forceCompleteLocationRequest(requestID)
        /*
         2016-11-14 16:42:20.298 1114-lm[2825:61742] INTULocationManager: Location services updates have started.
         2016-11-14 16:42:20.299 1114-lm[2825:61742] INTULocationManager: Location Request added with ID: 1
         2016-11-14 16:42:20.300 1114-lm[2825:61742] INTULocationManager: Location services updates have stopped.
         2016-11-14 16:42:20.310 1114-lm[2825:61742] INTULocationManager: Location Request completed with ID: 1, currentLocation: (null), achievedAccuracy: 0, status: 1
         1 //INTULocationStatusTimedOut
         
         typedef NS_ENUM(NSInteger, INTULocationStatus) {
         // These statuses will accompany a valid location.
         /** Got a location and desired accuracy level was achieved successfully. */
         INTULocationStatusSuccess = 0,
         /** Got a location, but the desired accuracy level was not reached before timeout. (Not applicable to subscriptions.) */
         INTULocationStatusTimedOut,
         // These statuses indicate some sort of error, and will accompany a nil location.
         /** User has not yet responded to the dialog that grants this app permission to access location services. */
         INTULocationStatusServicesNotDetermined,
         /** User has explicitly denied this app permission to access location services. */
         INTULocationStatusServicesDenied,
         /** User does not have ability to enable location services (e.g. parental controls, corporate policy, etc). */
         INTULocationStatusServicesRestricted,
         /** User has turned off location services device-wide (for all apps) from the system Settings app. */
         INTULocationStatusServicesDisabled,
         /** An error occurred while using the system location services. */
         INTULocationStatusError
         };
        */
        
        // Cancel the request (won't execute the block)
//        locMgr.cancelLocationRequest(requestID)
        /*
         2016-11-14 16:47:58.102 1114-lm[2918:64282] INTULocationManager: Location services updates have started.
         2016-11-14 16:47:58.103 1114-lm[2918:64282] INTULocationManager: Location Request added with ID: 1
         2016-11-14 16:47:58.104 1114-lm[2918:64282] INTULocationManager: Location Request canceled with ID: 1
         2016-11-14 16:47:58.105 1114-lm[2918:64282] INTULocationManager: Location services updates have stopped.
         */
 
        /*
        locMgr.subscribeToLocationUpdates(withDesiredAccuracy: .room){(currentLocation: CLLocation?, achievedAccuracy: INTULocationAccuracy, status: INTULocationStatus)  in
            if status == INTULocationStatus.success {
                print(currentLocation!)
                /*持续更新
                 2016-11-14 16:35:15.959 1114-lm[2749:59089] INTULocationManager: Location services updates have started.
                 2016-11-14 16:35:15.959 1114-lm[2749:59089] INTULocationManager: Location Request added with ID: 1
                 <+31.78583400,+121.40641700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 2016/11/14 中国标准时间 下午4:33:33
                 <+31.78583400,+121.40641700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 2016/11/14 中国标准时间 下午4:28:34
                 ...
                 */
            }else {
                print(status.rawValue)
            }
        }
        */
    }

}


 类似资料: