简体   繁体   English

Swift-当前位置的自己结构/类(CLLocationManager)

[英]Swift - Own struct/class for current location (CLLocationManager)

I want to create an own struct or class that fetches the users current location. 我想创建一个自己的结构或类来获取用户的当前位置。

It seems that I'm unable to use the CLLocationManager and it's functions (didUpdateLocations, didFailWithError, etc.) in a struct (Xcode makes all of the text color black and I get an error saying An internal error occurred. Source editor is limited. ). 看来我无法在结构中使用CLLocationManager及其功能(didUpdateLocations,didFailWithError等)(Xcode将所有文本颜色变为黑色,并且我收到一条错误消息,指出An internal error occurred. Source editor is limited. )。 When I use a class and try to use a "getPlacemark"-function (to get the placemark from the instanced class) or just get it directly from the instance created ("instancedClass.placemarkInClass"), but nothing works, I only get Thread 1: EXC_BAD_ACCESS . 当我使用一个类并尝试使用“ getPlacemark”功能(从实例化的类中获取地标)或直接从创建的实例中获取地标(“ instancedClass.placemarkInClass”)时,但是没有任何效果,我只会得到Thread 1: EXC_BAD_ACCESS

This is my last attempt on creating a class: 这是我最后一次创建类的尝试:

import Foundation
import MapKit

class UserLocation: NSObject, CLLocationManagerDelegate{

    var mLocationManager: CLLocationManager
    var placemark: CLPlacemark

    override init(){
        placemark = CLPlacemark()
        self.mLocationManager = CLLocationManager()
        self.mLocationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.mLocationManager.requestWhenInUseAuthorization()
        self.mLocationManager.startUpdatingLocation()
    }

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

        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error) -> Void in

            if (error != nil) {
                print("Error: " + (error?.localizedDescription)!)
                return
            }
            if (placemarks?.count > 0) {
                let pm = placemarks![0]
                self.placemark = pm
                print(self.placemark.areasOfInterest)
                self.mLocationManager.stopUpdatingLocation()
            }

        })

    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Error: " + (error.localizedDescription))
    }

    func getPlacemark() -> CLPlacemark{
        return self.placemark
    }

}

In my ViewController I make it an instance variable ( currentLocation = CurrentLocation() ) and try to use both 在我的ViewController中,将其设置为实例变量( currentLocation = CurrentLocation() ),然后尝试同时使用两者

let userPlacemark = currentLocation.getPlacemark() and let userPlacemark = currentLocation.placemark but nothing is working. 让userPlacemark = currentLocation.getPlacemark()并让userPlacemark = currentLocation.placemark但没有任何作用。

Is there a way to have an own class or struct to fetch the current location and use it in other ViewControllers? 有没有办法拥有自己的类或结构来获取当前位置并在其他ViewController中使用它?

There are two observations with your code: 您的代码有两个观察结果:

  1. You are not calling the [super init] in your init once you finish setting up the member variables. 一旦完成了成员变量的设置,就不会在init中调用[super init]。 Modify you init as 将您的init修改为

      init(){ placemark = CLPlacemark() mLocationManager = CLLocationManager() mLocationManager.desiredAccuracy = kCLLocationAccuracyBest mLocationManager.requestWhenInUseAuthorization() mLocationManager.startUpdatingLocation() super.init() mLocationManager.delegate = self } 
  2. Setting up delegate of location manager. 设置位置经理的代表。 You should do this in init soon after you create instance of location manager. 创建位置管理器实例后,应在init中立即执行此操作。 See the init code above. 参见上面的初始化代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM