swift - 即使在滚动时也将 MKPointAnnotation 修复到 MapKit 的中心

标签 swift xcode mapkit mapkitannotation

我想让 MKPointAnnotation 固定在 map 中心,即使在滚动时也是如此,我尝试做到这一点,但 MKPointAnnotation 在滚动时不会移动 这是我的代码:

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方法中调用stopUpdatingLocation()

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
    }
}

并且您应该使用 regionDidChangeAnimated 方法使 MKPointAnnotation 固定在 map 中心,即使在滚动时也是如此,

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)
}

关于swift - 即使在滚动时也将 MKPointAnnotation 修复到 MapKit 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51445611/

相关文章:

ios - 在 SKAction.run 中调用函数不起作用

xcode - 无法从 XCode Playground 中导入的框架访问类

swift - 在没有 pod 的情况下使用 JSQMessagesViewController

c++ - Xcode 不打印日志/控制台消息。显示黑色调试器

ios - 通过推送通知 Objective C iOS 从 Kill 状态打开时应用程序崩溃

swift - MKAnnotationView 缩放图像导致 calloutView 也缩放

ios - MKAnnotationView subview 上的 UITapGestureRecognizer 不起作用。 swift

ios - 如何在多个位置放置图钉 mapkit swift

php - xcode ios 将 nsarray 对象拆分为两个单独的对象

swift4 - 倒圆图填充swift 4