如何在 locationmanager 函数之外使用用户位置 swift 3

How to use user location out of locationmanager function swift 3

我是编码新手,遇到了这个问题。我正在基于 MapKit 制作 af trashcanfinder 应用程序,我想在地图上显示从用户位置到我的图钉的距离。我已经制作了将 CLLocationCoordinate2D 转换为 CLLocation 的函数,并且具有显示从一点到另一点的距离的函数,但我似乎无法使用 locationmanager 函数之外的用户位置。

这是我的代码:

 @IBOutlet weak var mapView: MKMapView!

let manager = CLLocationManager()


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

    let location = locations[0]
    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
    let myLocation = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)

    mapView.setRegion(region, animated: true)

    self.mapView.showsUserLocation = true


}



    func distancePointToPoint(location1: CLLocationCoordinate2D, location2: CLLocationCoordinate2D) -> String {
        let cllocation1 = converter(location: location1)
        let cllocation2 = converter(location: location2)
        let distance = cllocation1.distance(from: cllocation2)
        let shortDistance = distance - distance.truncatingRemainder(dividingBy: 0.1)                                   //makes sure there is only one number behind comma
        if shortDistance < 0 {
            return "The distance is \(shortDistance) meters"
        }
        else {
            return "The distance is \(shortDistance - (shortDistance / 1000).truncatingRemainder(dividingBy: 0.1)) kilometers"
        }
    }

所以我需要在 distancePointToPoint 函数中使用用户位置作为 location1。

我知道使用任何其他函数我都可以 return 值,但是我没有任何值可以给 locationmanager 函数,因为它会从设备本身获取它。

我已经做过研究,我发现的唯一其他方法是将 myLocation 变量放在函数外部,只更改函数内部的值,但是 xcode 开始抱怨 class 没有初始化程序,我不知道该怎么做。

我刚开始学代码,如果犯了愚蠢的错误,请见谅。

感谢任何帮助!

这里是 LocationManager didUpdate 方法 :

//MARK: LocationManager Delegates Methods
  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        //Get Current Location
        let location = locations.last! as CLLocation
        let userLocation:CLLocation = locations[0] as CLLocation

        //Save current lat long
        UserDefaults.standard.set(userLocation.coordinate.latitude, forKey: "LAT")
        UserDefaults.standard.set(userLocation.coordinate.longitude, forKey: "LON")
        UserDefaults().synchronize()
    }

然后创建一个函数并从 viewWillAppear 方法中调用该函数:

     func createPin(){

            //Access user Location LAT & LON from User Defaults
            let coordinate =  CLLocationCoordinate2D(latitude: UserDefaults.standard.value(forKey: "LAT") as! CLLocationDegrees, longitude: UserDefaults.standard.value(forKey: "LON") as! CLLocationDegrees)
        var region = MKCoordinateRegion(center: coordinate, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
        region.center = coordinate
        self.map.setRegion(region, animated: true)

//Also now you have coordintes know for USER Location you can add annotation too

    //Rest do your stuff 
    }

如有任何其他问题,请随时发表评论。谢谢