gpt4 book ai didi

swift - 无法设置 maprect 以显示所有注释

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

我有 2 个注释要显示在 mapview 上,但无法将 maprect 设置为在屏幕上显示所有注释而不要求用户缩小。

我尝试使用 showAnnotations 但没有成功。任何人都能够在 Swift 和 Xcode 6.1.1 中做到这一点?

这是我的代码:

class ViewController: UIViewController, MKMapViewDelegate {


@IBOutlet var map: MKMapView!

override func viewDidLoad() {
super.viewDidLoad()

var mapView = map
// 1
let point1 = CLLocationCoordinate2D(latitude: 38.915565, longitude: -77.093524)
let point2 = CLLocationCoordinate2D(latitude: 38.890693, longitude: -76.933318)

//2
let annotation = MKPointAnnotation()
annotation.setCoordinate(point1)
annotation.title = "point1"
map.addAnnotation(annotation)

let annotation2 = MKPointAnnotation()
annotation2.setCoordinate(point2)
annotation2.title = "point2"
map.addAnnotation(annotation2)

//3
// option1: set maprect to cover all annotations, doesn't work
var points = [annotation, annotation2]
var rect = MKMapRectNull
for p in points {
let k = MKMapPointForCoordinate(p.coordinate)
rect = MKMapRectUnion(rect, MKMapRectMake(k.x, k.y, 0.1, 0.1))
println("result: x = \(rect.origin.x) y = \(rect.origin.y)")
}

map.setVisibleMapRect(rect, animated: true)

// option 2: using showAnnotations, doesn't work
//map.showAnnotations(points, animated: true)
}

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


}

这是我目前得到的: enter image description here

这是我希望看到的: enter image description here

感谢您的帮助。

最佳答案

终于找到为什么标注的引脚一直没有显示在屏幕可见区域的原因了。我认为 MapKit 框架的行为与以前的版本有点不同。由于我使用自动布局允许 map 扩展到所有设备(iPhone、iPad)的整个屏幕, map 的 setVisibleMapRect 或 mapView.showAnnotations 应该在 mapViewDidFinishLoadingMap 中调用,而不是在 View Controller 的 viewDidLoad 中调用

例如:

 func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
// this is where visible maprect should be set
mapView.showAnnotations(mapView.annotations, animated: true)
}

关于swift - 无法设置 maprect 以显示所有注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28154774/

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