gpt4 book ai didi

swift - MKMapView 边界

转载 作者:行者123 更新时间:2023-11-30 10:42:14 28 4
gpt4 key购买 nike

我将从 Google map 转到 Apple map 。 Google map 能够根据东北和西南坐标更新相机,如下所示:

let bounds = GMSCameraUpdate.fit(GMSCoordinateBounds(northEastSouthWestBounds), with: .zero)
self.mapView.moveCamera(bounds)

我知道我可以使用 setVisibleMapRect(_:animated:) 并且它接受 MKMapRect。我真正的问题是如何根据东北坐标(CLLocation)和西南坐标(CLLocation)创建MKMapRect

最佳答案

构造一个MKMapRect从您的坐标并将其传递给 setVisibleMapRect(_:animated:)setVisibleMapRect(_:edgePadding:animated:) .

要从不同点类型的数组创建 MKMapRect:

import MapKit

extension Array where Element == CLLocationCoordinate2D {
func mapRect() -> MKMapRect? {
return map(MKMapPoint.init).mapRect()
}
}

extension Array where Element == CLLocation {
func mapRect() -> MKMapRect? {
return map { MKMapPoint($0.coordinate) }.mapRect()
}
}

extension Array where Element == MKMapPoint {
func mapRect() -> MKMapRect? {
guard count > 0 else { return nil }

let xs = map { $0.x }
let ys = map { $0.y }

let west = xs.min()!
let east = xs.max()!
let width = east - west

let south = ys.min()!
let north = ys.max()!
let height = north - south

return MKMapRect(x: west, y: south, width: width, height: height)
}
}

关于swift - MKMapView 边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56549632/

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