ios - 返回具有之前存在的相同方向的 View

标签 ios swift

好吧,我猜这个有点大......

我正在制作一个应用程序,其中将有一个 MapKit 和一个按钮。当用户单击该按钮时,他/她将打开一个新的 ViewController,其中包含多个目的地选项。确认他想去的地方后,MapKit View 将重新打开并显示特定地点的路线。为此,我在链接到 MapKit View 的 ViewController 上创建了一个函数:

func createmap(latit: Double, longit: Double){

    //set what's going to show up in the Map
    MapView.delegate = self
    MapView.showsScale = true
    MapView.showsPointsOfInterest = true
    MapView.showsUserLocation = true

    //request authorization for user location data storage
    locationManager.requestAlwaysAuthorization()
    locationManager.requestWhenInUseAuthorization()

    //if authorization is given, use the user location
    if CLLocationManager.locationServicesEnabled(){

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()

    }

    //set source and destination coordinates of direction recommendation
    let sourceCoordinates = locationManager.location?.coordinate
    let destinationCoords = CLLocationCoordinate2D(latitude: latit, longitude: longit)

    //set placemarks with source and destination coordinates
    let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates!)
    let destPlacemark = MKPlacemark(coordinate: destinationCoords)

    //put placemarks on maps with the source and destination coordinates
    let sourceItem = MKMapItem(placemark: sourcePlacemark)
    let destItem = MKMapItem(placemark: destPlacemark)

    //set direction request, source and destination request coordinates and transport type
    let directionRequest = MKDirectionsRequest()
    directionRequest.source = sourceItem
    directionRequest.destination = destItem
    directionRequest.transportType = .automobile

    //set response if sucess or error
    let directions = MKDirections(request: directionRequest)
    directions.calculate(completionHandler: {

        response, error in
        guard let response = response else {
            if let error = error {
                print("Something went wrong.")
            }
            return
        }

        //set format of route line

        let route = response.routes[0]
        self.MapView.add(route.polyline, level: .aboveRoads)

        //set map framing
        let rekt = route.polyline.boundingMapRect
        self.MapView.setRegion(MKCoordinateRegionForMapRect(rekt), animated: true)


    })

}

然后,为了让这个函数运行,我这样做了:

 override func viewDidLoad() {
    super.viewDidLoad()

        createmap(latit: lat, longit: long)

}

lat 和 long 是公开声明的变量。

在另一个 View 中,我有一个名为“返回 map ”的按钮,我尝试在其中进行简单的转场。但是,此代码存在一个问题:如果我得到某个地方的路线,重新打开另一个 View ,然后按“返回 map ”,设置的路线就会消失。我不知道该怎么做才能让他们留下来,有人可以帮忙吗?

更多信息:“返回 map ”按钮作为一个简单的 segue 链接到 MapKit View (我单击、拖放到 MapKit View ,没有编写任何代码行)

最佳答案

我认为问题是您的 Map VC 已经添加到导航堆栈中,因此不会再次调用 ViewDidLoad 方法您应该将代码从 viewdidload 移动到 viewWill 看起来像

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)

        createmap(latit: lat, longit: long)

    }

关于ios - 返回具有之前存在的相同方向的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45364005/

相关文章:

ios - iOS-将 “objects”添加到现有应用中(越狱)

ios - 如何在 Swift 2.0 中将 UITextField 转换为整数?

iphone - 在cocos2d中启动和停止粒子系统

ios - 位置返回零

ios - MPMoviePlayerController 无法播放本地视频

UILabel继承的Swift自定义UIView

swift - 如何在 Firestore (Swift) 中存储数据

ios - 无法将类型 'NSFetchRequest<NSFetchRequestResult>' 的值转换为指定类型 'NSFetchRequest<T>'

ios - UIStackView 没有显示阴影

swift - ?在引擎盖下是 Optional<T> ,隐式展开可选的怎么样 - 有它的表示吗?