Swift MapView困在用户的当前位置

时间:2016-12-17 05:02:19

标签: swift mkmapview

目前,我的代码会在用户的当前位置删除一个图钉。当我尝试移动地图时会出现一个小问题,因为视图将向后移动并以当前位置引脚为中心。我希望用户能够导航地图并移动它,如果用户切换视图控制器(转到另一个选项卡)并返回,则地图将以用户位置引脚为中心。我一直在尝试修改此代码来执行此操作,但我没有任何运气从哪里开始。

import UIKit
import MapKit
import CoreLocation
let newPin = MKPointAnnotation()

class MapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {



@IBOutlet weak var map: MKMapView!

 let locationManager =  CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    // User's location

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    if #available(iOS 8.0, *) {
    locationManager.requestAlwaysAuthorization()
    } else {
    // Fallback on earlier versions
    }
    locationManager.startUpdatingLocation()


    // add gesture recognizer
    let longPress = UILongPressGestureRecognizer(target: self, action: #selector(MapVC.mapLongPress(_:))) // colon needs to pass through info
    longPress.minimumPressDuration = 1.5 // in seconds
    //add gesture recognition
    map.addGestureRecognizer(longPress)
}

// func called when gesture recognizer detects a long press

func mapLongPress(_ recognizer: UIGestureRecognizer) {

    print("A long press has been detected.")

    let touchedAt = recognizer.location(in: self.map) // adds the location on the view it was pressed
    let touchedAtCoordinate : CLLocationCoordinate2D = map.convert(touchedAt, toCoordinateFrom: self.map) // will get coordinates

    let newPin = MKPointAnnotation()
    newPin.coordinate = touchedAtCoordinate
    map.addAnnotation(newPin)


}

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


    map.removeAnnotation(newPin)

    let location = locations.last! as CLLocation

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    //set region on the map
    map.setRegion(region, animated: true)

    newPin.coordinate = location.coordinate
    map.addAnnotation(newPin)





}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

1 个答案:

答案 0 :(得分:1)

您的代码有几个问题:

您在全局范围内声明变量newPin,并在mapLongPress(...)中在本地声明新变量let newPin = ...,因此不会使用全局newPin

didUpdateLocations()中,您首先删除(全局)newPin注释(为什么??)并在函数末尾再次设置它。因为全局newPin从未设置为任何有用的东西,所以永远不会得到所需的结果。

此外,在didUpdateLocations()中,您可以将地图的区域和中心点设置为当前位置。这是在每次更新位置时完成的,在尝试平移地图时会产生奇怪的结果。

要在视图显示时设置中心和区域,请尝试以下操作:

class MapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
    @IBOutlet weak var map: MKMapView!
    let locationManager =  CLLocationManager()

    // class variable for the current location
    var lastLocation: CLLocation?

    override func viewDidLoad() {
        // ...
    }

    override func viewDidAppear(_ animated: Bool) {
        if self.lastLocation != nil {
            // set center and region to current location
            let center = CLLocationCoordinate2D(latitude: self.lastLocation.coordinate.latitude, longitude: self.lastLocation.coordinate.longitude)
            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

            //set region on the map
            map.setRegion(region, animated: true)
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        self.lastLocation = locations.last
    }

}