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

swift – 无法设置maprect以显示所有注释,第1张

概述我有2个注释显示在mapview上,但无法设置maprect以在屏幕上显示所有这些注释而无需用户缩小. 我试过showAnnotations但没有运气.任何人都可以在Swift和Xcode 6.1.1中做到这一点? 这是我的代码: class ViewController: UIViewController, MKMapViewDelegate { @IBOutlet var map 我有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.    }     }

这就是我目前所得到的:

这是我期望看到的:

谢谢你的帮助.

我终于找到了为什么注释的引脚没有显示在屏幕的可见区域中.我认为MapKit框架的行为与以前的版本略有不同.由于我使用autolayout允许地图扩展到所有设备(iPhone,iPad)的整个屏幕,因此应在mapVIEwDIDFinishLoadingMap中调用setVisibleMapRect或mapVIEw.showAnnotations,而不是在视图控制器的vIEwDIDLoad中调用

例如:

func mapVIEwDIDFinishLoadingMap(_ mapVIEw: MKMapVIEw) {     // this is where visible maprect should be set     mapVIEw.showAnnotations(mapVIEw.annotations,animated: true)   }
总结

以上是内存溢出为你收集整理的swift – 无法设置maprect以显示所有注释全部内容,希望文章能够帮你解决swift – 无法设置maprect以显示所有注释所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1033170.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存