Swift 拟合注释 [英] Swift fit annotations

查看:34
本文介绍了Swift 拟合注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个正在查询的位置在解析中.它显示所有注释,但只缩放最后一个.如何找到最大和最小纬度和经度并使它们适合?

I have a few locations in parse which I'm query-ing. It shows all annotations but it only zoom the last one. How can I find max and min latitudes and longitudes and make them fit ?

stackoverflow 上有很多这些,但它们几乎都在 Objective-c 中.

There is plenty of these on stackoverflow but they're almost all in objective-c.

推荐答案

Objective-C 中的示例本质上仍然有效,因为底层 SDK/API 仍然相同——只是使用不同的语言调用(并且总是文档).

The examples in Objective-C are still essentially valid since the underlying SDK/API is still the same -- just being called using a different language (and there's always the documentation).

要显示所有注释,基本上有两种方法:

To show all annotations, there are essentially two ways to do it:

  1. 使用方便的showAnnotations 方法.您将一组注释传递给它,它会自动计算要显示的合理区域.在添加所有注释后调用它(在 for 循环之后).事实上,showAnnotations 甚至会添加注释本身,如果它们还没有出现在地图上.要显示地图上已经存在的所有注释,只需将地图自己的 annotations 数组传递给它.示例:

  1. Use the convenient showAnnotations method. You pass it an array of annotations and it will automatically calculate a reasonable region to display. You call it after adding all the annotations (after your for loop). In fact, showAnnotations will even add the annotations itself if they aren't already on the map. To show all annotations that are already on the map, just pass it the map's own annotations array. Example:

self.mapView.showAnnotations(self.mapView.annotations, animated: true)

  • 如果您对 showAnnotations 计算的默认区域不满意,您可以自己计算一个.而不是计算最小/最大纬度和经度等,我更喜欢使用内置的 MKMapRectUnion 函数来创建一个适合所有注释的 MKMapRect 然后调用 setVisibleMapRect(mapRect,edgePadding,animated) 可以方便地在拟合的地图矩形周围的屏幕点中定义填充.这种技术最初是在 Apple 的一个非常早期的地图视图示例应用程序中发现的.示例:

  • If you are not happy with the default region calculated by showAnnotations, you can calculate one yourself. Instead of calculating minimum/maximum latitudes and longitudes, etc, I prefer to use the built-in MKMapRectUnion function to create an MKMapRect that fits all annotations and then call setVisibleMapRect(mapRect,edgePadding,animated) which lets one conveniently define padding in screen points around the fitted map rect. This technique was originally found in a very early map view sample app from Apple. Example:

    var allAnnMapRect = MKMapRectNull
    
    for object in objects! {
        //existing code that creates annotation...
        //existing code that calls addAnnotation...
    
        let thisAnnMapPoint = MKMapPointForCoordinate(annotation.coordinate)
        let thisAnnMapRect = MKMapRectMake(thisAnnMapPoint.x, thisAnnMapPoint.y, 1, 1)
        allAnnMapRect = MKMapRectUnion(allAnnMapRect, thisAnnMapRect)
    }
    
    //Set inset (blank space around all annotations) as needed...
    //These numbers are in screen CGPoints...
    let edgeInset = UIEdgeInsetsMake(20, 20, 20, 20)
    
    self.mapView.setVisibleMapRect(allAnnMapRect, edgePadding: edgeInset, animated: true)
    

  • 这篇关于Swift 拟合注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆