不支持叠加,但您可以轻松调整大小View
/Circle
通过计算之间的比率View
和span
首先,由于您需要公里,因此您可以计算该区域的上边缘和下边缘(纬度跨度)之间的距离。
extension MKCoordinateRegion{
///Identify the length of the span in meters north to south
var spanLatitude: Measurement<UnitLength>{
let loc1 = CLLocation(latitude: center.latitude - span.latitudeDelta * 0.5, longitude: center.longitude)
let loc2 = CLLocation(latitude: center.latitude + span.latitudeDelta * 0.5, longitude: center.longitude)
let metersInLatitude = loc1.distance(from: loc2)
return Measurement(value: metersInLatitude, unit: UnitLength.meters)
}
}
using GeometryReader
你可以得到height
of the View
/Map
,然后是比率。一旦你知道了每米的大小,只需乘以千米即可。
//Size per meter
let ratio = (geo.size.height/region.spanLatitude.value)
//Size per kilometer
let kilometerSize = ratio * 1000
然后使用kilometerSize
in the frame
.
考虑到两者之间可能存在不一致,这可能不是完美的公里数View
和Map
但可能非常接近。如果您能找到已知公里数的东西,您可以对其进行测试。
struct ScaledAnnotationView: View {
static let usersLocation = CLLocationCoordinate2D(latitude: 52.0929779694589, longitude: 5.084964426384347)
@State private var region = MKCoordinateRegion(
center: ScaledAnnotationView.usersLocation,latitudinalMeters: 1000, longitudinalMeters: 1000
)
let annotations = [CurrentUsersAnnotation()]
var body: some View {
//Get the size of the frame for scale
GeometryReader{ geo in
Map(
coordinateRegion: $region, annotationItems: annotations) { _ in
MapAnnotation(coordinate: ScaledAnnotationView.usersLocation) {
//Size per kilometer or any unit, just change the converted unit.
let kilometerSize = (geo.size.height/region.spanLatitude.converted(to: .kilometers).value)
Circle()
.fill(Color.red.opacity(0.5))
//Keep it a circle
.frame(width: kilometerSize, height: kilometerSize)
}
}
}
}
}
![enter image description here](https://i.stack.imgur.com/lZedI.jpg)
这是谷歌地图距离测量,用于确认测量
![enter image description here](https://i.stack.imgur.com/Dp6ib.png)