如何在 iOS 中使用多个注释来增加 Apple Mapkit 的缩放

Posted

技术标签:

【中文标题】如何在 iOS 中使用多个注释来增加 Apple Mapkit 的缩放【英文标题】:How can i increase zoom in Apple Mapkit with multiple annotations in iOS 【发布时间】:2021-07-29 13:20:14 【问题描述】:

我试过这段代码,在这里我将 MKCoordinateSpan 增加到 200 - 200,但是我的应用程序崩溃了,有人可以帮助我。

 func setupMap(hotelListData:[HotelListData], cityLocation: 
                CLLocationCoordinate2D )
   

    let coordinateRegion = MKCoordinateRegion(center: cityLocation, span: MKCoordinateSpan(latitudeDelta: 200, longitudeDelta: 200))
    mapView.setRegion(coordinateRegion, animated: true)

    //Set Multiple Annotation
    for data in hotelListData 
        let annotation = MKPointAnnotation()
        annotation.title = data.hotel?.name
        annotation.coordinate = CLLocationCoordinate2D(latitude: Double(data.hotel?.latitude ?? 0.0), longitude: Double(data.hotel?.longitude ?? 0.0))
        mapView.addAnnotation(annotation)
    

更新:

我将跨度从(latitudinalMeters 和纵向Meters)更改并得到了预期的结果:-

现在可以在这里找到:

i)为苹果地图设置多个注释。

ii) 使用所需位置调整缩放。

  func setupMap(hotelListData:[HotelListData], cityLocation: 
                 CLLocationCoordinate2D )
    
    let coordinateRegion = MKCoordinateRegion(center: cityLocation, latitudinalMeters: CLLocationDistance(exactly: 15000)!, longitudinalMeters: CLLocationDistance(exactly: 15000)!)
    mapView.setRegion(coordinateRegion, animated: true)

    //Set Multiple Annotation
    for data in hotelListData 
        let annotation = MKPointAnnotation()
        annotation.title = data.hotel?.name
        annotation.coordinate = CLLocationCoordinate2D(latitude: Double(data.hotel?.latitude ?? 0.0), longitude: Double(data.hotel?.longitude ?? 0.0))
        mapView.addAnnotation(annotation)
    

【问题讨论】:

嗯...地球上只有180度的纬度。你在200度的纬度范围内做什么?你想放大,不是吗? @Sweeper 是的,我想放大,我想增加或减少它会影响缩放,你能帮我吗,我到底需要做什么才能缩放。 是的,改变它会影响缩放。但是你需要把它改成一个合理的数字。你想放大多远?地图应该跨越多少纬度和经度? 我需要将它放大到可以看到城市中特定区域的位置,以便用户能够清楚地看到所有注释。我需要传递什么数字才能得到结果? @ElTomato,我尝试将每个设置为 100.0,但现在没有得到想要的结果(确切地说:5000)!) 希望它会起作用。 【参考方案1】:

MKCoordinateSpanlongitudeDeltalatitudeDelta 以度为单位。从北极到南极只有180度的纬度,所以用200这个参数不太明智。

由于您只想在地图上显示一个城市的区域,如果您知道您的应用处理的城市通常有多大,您可以使用this other initialiser 以米为单位。

例如,

let coordinateRegion = MKCoordinateRegion(center: cityLocation, latitudinalMeters: 30000, longitudinalMeters: 30000)

如果您的城市大小不一,或者您不知道它们有多大,那么另一种方法是计算酒店的经纬度范围,并使用它来创建MKCoordinateSpan

var minLat: CLLocationDegrees = 90
var maxLat: CLLocationDegrees = -90
var minLong: CLLocationDegrees = 180
var maxLong: CLLocationDegrees = -180
for data in hotelListData 
    // ... you annotation code ...

    guard let hotel = data.hotel else  continue 
    if hotel.latitude < minLat  minLat = hotel.latitude 
    if hotel.latitude > maxLat  maxLat = hotel.latitude 
    if hotel.longitude < minLong  minLong = hotel.longitude 
    if hotel.longitude > maxLong  maxLong = hotel.longitude 

let latRange = max(0.01, maxLat - minLat) // if the range is too small, make it at least 0.01
let longRange = max(0.01, maxLong - minLong)
let coordinateRegion = MKCoordinateRegion(
                           center: cityLocation,
                           span: MKCoordinateSpan(latitudeDelta: latRange, longitudeDelta: longRange)

【讨论】:

以上是关于如何在 iOS 中使用多个注释来增加 Apple Mapkit 的缩放的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS 中使用多个点在 Apple 地图中显示路线

Apple IOS之ATS问题

使用 Swift 3 在 iOS 10 中将图像从服务器加载到 Apple Map 中的注释视图

如何在 Selenium 中使用 @FindBy 注释来处理跨度文本?

SwiftUI 4.0为Toggle视图在iOS 16中增加属性多选功能

SwiftUI 4.0为Toggle视图在iOS 16中增加属性多选功能