【问题标题】:how to calculate MKMapSize from CGSize如何从 CGSize 计算 MKMapSize
【发布时间】:2018-03-14 09:14:14
【问题描述】:

我正在使用 MapKit,我在其中关注 MKAnnotationView,其中绘制了一个红色圆圈 CGSize(width: 26, height: 26)

如何从CGSizeMKAnnotationView 计算大小MKMapSize

来源

我得到MKMapView 和几个MKOverlays,当用户点击任何覆盖时,mapViews 区域将更改为该覆盖边界,这工作正常,但如果点击坐标,我需要停止更改区域位于MKAnnotationView(红色圆圈)所在的坐标区域内。

我在这里寻找其他答案但没有运气并尝试过:

let zoomLevel = log2(360 * ((Double(self.frame.size.width) / 256) / mapView.region.span.longitudeDelta)) - 1
MKMapSize(width: 26 * zoomLevel, height: 26 * zoomLevel)

【问题讨论】:

  • 你的红圈是annotationView还是overlay?
  • 是annotationView
  • 您是否尝试过使用func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) 委托方法?
  • 是的,我确实尝试过这个解决方法并确定它不起作用,因为当 annotationView 的 calloutView 打开时,这个方法不会调用

标签: ios mapkit coordinate-transformation


【解决方案1】:

这里是MKMapView 方法,它可以将CGSize 转换为MKCoordinateRegion

mapView.convert(rect: CGRect, toRegionFrom: UIView?)

用法:

let customAnnotationSize = CGSize(width: 26, height: 26)
let region = mapView.convert(CGRect(origin: .zero, size: customAnnotationSize), toRegionFrom: mapView)

拥有region 很重要,最后我检查注解的坐标是否在region 内。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多