gpt4 book ai didi

ios - 当用户滚动 map 时,将图钉(MKAnnotation)保持在中心

转载 作者:搜寻专家 更新时间:2023-10-31 21:48:43 40 4
gpt4 key购买 nike

当用户像 Careem 应用程序一样滚动 map 时,我希望将 MKAnnotaion 保持在屏幕中央:

enter image description here

到目前为止,我设法显示了图钉,更新了图钉位置,但是当我滚动 map 时,代码中的注释最初移动然后回到中心。我希望大头针保持在中心位置。

  @IBOutlet var mapView: MKMapView!
var centerAnnotation = MKPointAnnotation()
var manager:CLLocationManager!

override func viewDidLoad() {
super.viewDidLoad()
manager = CLLocationManager() //instantiate
manager.delegate = self // set the delegate
manager.desiredAccuracy = kCLLocationAccuracyBest // required accurancy

manager.requestWhenInUseAuthorization() // request authorization
manager.startUpdatingLocation() //update location

var lat = manager.location.coordinate.latitude // get lat
var long = manager.location.coordinate.longitude // get long
var coordinate = CLLocationCoordinate2DMake(lat, long)// set coordinate

var latDelta:CLLocationDegrees = 0.01 // set delta
var longDelta:CLLocationDegrees = 0.01 // set long
var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var region:MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)

self.mapView.setRegion(region, animated: true)
centerAnnotation.coordinate = mapView.centerCoordinate
self.mapView.addAnnotation(centerAnnotation)
}

func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
centerAnnotation.coordinate = mapView.centerCoordinate;
}

****更新****

按照 Anna 的建议,我在 map 上添加了一个 View 。这里的代码:

var newPoint = self.mapView.convertCoordinate(mapView.centerCoordinate, toPointToView: self.view)



var pinImage = UIImage(named: "yoga_pins.png")
var imageView = UIImageView(image: pinImage) // set as you want

imageView.image = pinImage
imageView.backgroundColor = UIColor.clearColor()
imageView.contentMode = UIViewContentMode.Center
imageView.center.y = newPoint.y
imageView.center.x = newPoint.x


self.view.addSubview(imageView)

唯一的问题是,当第一次加载 map 时,位于 mapView.centerCoordinate 上的注释,我将使用它来获取纬度和经度:

enter image description here

然后当我滚动 map 时,图钉移动到正确的位置(在图像下方):

    func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
centerAnnotation.coordinate = mapView.centerCoordinate;

}

enter image description here

最佳答案

您可以使用自定义按钮。在 map 中心添加该按钮。只需根据您的条件显示和隐藏该按钮即可。因此,当您移动 map 时,该按钮会停留在 map 中心的同一位置。 see centre button sedan not available.

关于ios - 当用户滚动 map 时,将图钉(MKAnnotation)保持在中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29771715/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com