Swift 将MKPointAnnotation固定到MapKit的中心,即使在滚动时也是如此
我想让MKPointAnnotation即使在滚动时也固定在地图的中心,我尝试使其固定,但滚动时MKPointAnnotation不移动 这是我的密码:Swift 将MKPointAnnotation固定到MapKit的中心,即使在滚动时也是如此,swift,xcode,mapkit,mapkitannotation,Swift,Xcode,Mapkit,Mapkitannotation,我想让MKPointAnnotation即使在滚动时也固定在地图的中心,我尝试使其固定,但滚动时MKPointAnnotation不移动 这是我的密码: import UIKit import MapKit class HomeVC: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate { @IBOutlet var myMap: MKMapView! private var locationManager = CLLoc
import UIKit
import MapKit
class HomeVC: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {
@IBOutlet var myMap: MKMapView!
private var locationManager = CLLocationManager();
private var userLocation: CLLocationCoordinate2D?;
// private var riderLocation: CLLocationCoordinate2D
override func viewDidLoad() {
super.viewDidLoad()
initializeLocationManager()}
// find location on the map
private func initializeLocationManager(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// if we hade the coordinate from the manager
if let location = locationManager.location?.coordinate {
userLocation = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
// the place the map will show (ZOOM LVL ON MAP)
let region = MKCoordinateRegion(center: userLocation!, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
myMap.setRegion(region, animated: true)
//To Remove annotation = Point on map befor add new one
myMap.removeAnnotations(myMap.annotations)
//Show My Point At Map
let annotation = MKPointAnnotation();
annotation.coordinate = myMap.centerCoordinate
myMap.addAnnotation(annotation)
}
}
}
请尝试以下代码:
// Show My Point At Map
let annotation = MKPointAnnotation()
annotation.coordinate = myMap.region.center // instead of myMap.centerCoordinate
myMap.addAnnotation(annotation)
编辑:
您需要在didUpdateLocations
方法中调用stopUpdateLocation()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// if we hade the coordinate from the manager
if let location = locationManager.location?.coordinate {
userLocation = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
// the place the map will show (ZOOM LVL ON MAP)
let region = MKCoordinateRegion(center: userLocation!, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
myMap.setRegion(region, animated: true)
locationManager.stopUpdatingLocation() // <- Stop updating
}
}
谢谢你的回答,它是有效的,但当我试图在地图上滚动它移动不到1秒,并回到我开始的同一地区,你知道如何解决这个问题,再次感谢你的帮助
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
// To Remove annotation = Point on map befor add new one
myMap.removeAnnotations(myMap.annotations)
let annotation = MKPointAnnotation();
annotation.coordinate = myMap.region.center
myMap.addAnnotation(annotation)
}