首页 > 解决方案 > 无法获取更新的坐标以将它们附加到 SWIFT 4 中的数组

问题描述

我正在尝试存储actualRouteInUseAnnotation: [MKAnnotation]路线的开始和结束位置,以及路线的actualRouteInUseCoordinates : [CLLocationCoordinate2D]所有坐标。注释数组得到它的值,但坐标数组没有。

使用的功能是:开始:

func startTracking2() {

        self.trackingIsActive = true
        locationManager.stopUpdatingLocation()
        let startRoutePosition = RouteAnnotation(title: "Route Start", coordinate: (locationManager.location?.coordinate)!, imageName: "Route Start")
        self.actualRouteInUseAnnotations.append(startRoutePosition)
//        self.actualRouteInUseCoordinates.append((locationManager.location?.coordinate)!)
        print(actualRouteInUseCoordinates)
        print(actualRouteInUseAnnotations)
    }

对于停止:

func stopTracking2() {

        self.trackingIsActive = false
        locationManager.stopUpdatingLocation()
        let stopRoutePosition = RouteAnnotation(title: "Route Stop", coordinate: (locationManager.location?.coordinate)!, imageName: "Route Stop")
        self.actualRouteInUseAnnotations.append(stopRoutePosition)
        print(actualRouteInUseCoordinates)
        print(actualRouteInUseAnnotations)

    } 

对于更新的坐标:

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let mostRecentLocation = locations.last else { return }


//        setCoordinates() // set actual user coordinates
//        storeUserLocation() // store auctual user coordinates

        self.actualRouteInUseCoordinates.append(mostRecentLocation.coordinate)


    }

你能明白为什么我没有将更新的坐标附加到数组中吗?

标签: iosswiftmkmapviewcllocationmanagercllocationcoordinate2d

解决方案


推荐阅读