gpt4 book ai didi

ios - 以编程方式移动 MapView

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:36 26 4
gpt4 key购买 nike

我有一个 mapView,我在其中显示了一个位置(由自定义图钉表示),如屏幕截图所示 enter image description here

如何移动 mapView 以使图标完全可见?

最佳答案

要实现这一点,您首先需要计算屏幕 View 上要滚动的点(在您的情况下,假设它距离注释标记 20 像素),然后您需要将此点隐藏到 map 位置,以便您可以将 map 中心移动到该位置;-)。以下是在 MKMapView 委托(delegate) 方法中用 Swift 编写的代码,其中注释被点击。

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let pinLocation = view.annotation.coordinate

let currentCoordinates = mapView.centerCoordinate // Temporary saved map current center position

// Temp set map center position to pin location
mapView.centerCoordinate = pinLocation

let viewCenter = self.view.center
let fakecenter = CGPoint(x: viewCenter.x, y: viewCenter.y - 20) // point just up to the center point

// convert calculetd point to map location co-ordinates
let coordinate: CLLocationCoordinate2D = mapView.convert(fakecenter, toCoordinateFrom: self.view)

// reset to previous potion so thet animation start from that
mapView.centerCoordinate = currentCoordinates
self.mapView.setCenter(coordinate, animated: true) // change the new center
}

关于ios - 以编程方式移动 MapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18513520/

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