use*_*188 16 xcode core-location cllocationmanager ios
我有多个视图控制器需要获取用户的位置,所以我想创建一个单独的类,ViewControllers可以调用该类来获取用户的最新位置.
locationManager:didUpdateToLocation:fromLocation返回void.一旦计算出用户的纬度和经度,我如何将纬度和经度数据传回我的ViewControllers?
我可以尝试在我的locationManaging类中编写getter和setter,但是如果我这样做,我怎么知道何时从我的ViewController类中调用latitude getter和longitude getter方法?如何保持ViewController的主线程等待来自locationManaging类的纬度和经度值?
谢谢!
eri*_*ell 23
正如user1071136所说,单身人士的位置管理员可能就是你想要的.创建一个类,NSObject的子类,只有一个属性,a CLLocationManager
.
LocationManagerSingleton.h:
#import <MapKit/MapKit.h>
@interface LocationManagerSingleton : NSObject <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager* locationManager;
+ (LocationManagerSingleton*)sharedSingleton;
@end
Run Code Online (Sandbox Code Playgroud)
LocationManagerSingleton.m:
#import "LocationManagerSingleton.h"
@implementation LocationManagerSingleton
@synthesize locationManager;
- (id)init {
self = [super init];
if(self) {
self.locationManager = [CLLocationManager new];
[self.locationManager setDelegate:self];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
[self.locationManager setHeadingFilter:kCLHeadingFilterNone];
[self.locationManager startUpdatingLocation];
//do any more customization to your location manager
}
return self;
}
+ (LocationManagerSingleton*)sharedSingleton {
static LocationManagerSingleton* sharedSingleton;
if(!sharedSingleton) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedSingleton = [LocationManagerSingleton new];
}
}
return sharedSingleton;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//handle your location updates here
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
//handle your heading updates here- I would suggest only handling the nth update, because they
//come in fast and furious and it takes a lot of processing power to handle all of them
}
@end
Run Code Online (Sandbox Code Playgroud)
要获取最近收到的位置,只需使用[LocationManagerSingleton sharedSingleton].locationManager.location
.可能需要几秒钟来预热GPS以获得准确的位置.
use*_*136 15
创建一个具有latitude
和longitude
属性的单例类,startLocating
和endLocating
.在类中,创建一个CLLocationManager
实例,并将其委托设置为单例.在startLocating
和中endLocating
,调用CLLocationManager
实例的相应方法.使委托方法更新latitude
和longitude
属性.在其他方面ViewControllers
,阅读这个单身人士latitude
和longitude
属性.
要知道何时从另一个属性读取这些属性ViewController
,请在这些属性上设置观察者(请参阅NSKeyValueObserving协议参考
在此之前,请查找Internets以获取现有代码.
执行此操作后,使用许可许可证将其上载到GitHub.
根据上面的答案,这就是我所做的,你可以在github上找到完整的例子https://github.com/irfanlone/CLLocationManager-Singleton-Swift
只需在项目中导入此文件,然后您可以选择实施LocationUpdateProtocol或收听位置更新通知
import MapKit
protocol LocationUpdateProtocol {
func locationDidUpdateToLocation(location : CLLocation)
}
/// Notification on update of location. UserInfo contains CLLocation for key "location"
let kLocationDidChangeNotification = "LocationDidChangeNotification"
class UserLocationManager: NSObject, CLLocationManagerDelegate {
static let SharedManager = UserLocationManager()
private var locationManager = CLLocationManager()
var currentLocation : CLLocation?
var delegate : LocationUpdateProtocol!
private override init () {
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLLocationAccuracyHundredMeters
locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
}
// MARK: - CLLocationManagerDelegate
func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
currentLocation = newLocation
let userInfo : NSDictionary = ["location" : currentLocation!]
dispatch_async(dispatch_get_main_queue()) { () -> Void in
self.delegate.locationDidUpdateToLocation(self.currentLocation!)
NSNotificationCenter.defaultCenter().postNotificationName(kLocationDidChangeNotification, object: self, userInfo: userInfo as [NSObject : AnyObject])
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
class ViewController: UIViewController, LocationUpdateProtocol {
var currentLocation : CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "locationUpdateNotification:", name: kLocationDidChangeNotification, object: nil)
let LocationMgr = UserLocationManager.SharedManager
LocationMgr.delegate = self
}
// MARK: - Notifications
func locationUpdateNotification(notification: NSNotification) {
let userinfo = notification.userInfo
self.currentLocation = userinfo!["location"] as! CLLocation
print("Latitude : \(self.currentLocation.coordinate.latitude)")
print("Longitude : \(self.currentLocation.coordinate.longitude)")
}
// MARK: - LocationUpdateProtocol
func locationDidUpdateToLocation(location: CLLocation) {
currentLocation = location
print("Latitude : \(self.currentLocation.coordinate.latitude)")
print("Longitude : \(self.currentLocation.coordinate.longitude)")
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18638 次 |
最近记录: |