gpt4 book ai didi

ios - 仅本地图靠近屏幕边缘时,如何才能将 map 置于注释 View 的中心?

转载 作者:行者123 更新时间:2023-11-28 06:31:26 29 4
gpt4 key购买 nike

在我的 swift 应用程序中,我正在使用 MapKit 并向 map 添加一些注释。

目前,感谢这个问题:Trying to get the span size in meters for an iOS MKCoordinateSpan我设法创建了一个功能,当用户按下它时,它会将我的 map 集中在注释上。这是它的代码:

let span = mapView.region.span
let centerView = mapView.region.center

let loc1 = CLLocation(latitude: centerView.latitude - span.latitudeDelta * 0.5, longitude: centerView.longitude)
let loc2 = CLLocation(latitude: centerView.latitude + span.latitudeDelta * 0.5, longitude: centerView.longitude)
let loc3 = CLLocation(latitude: centerView.latitude, longitude: centerView.longitude - span.longitudeDelta * 0.5)
let loc4 = CLLocation(latitude: centerView.latitude, longitude: centerView.longitude + span.longitudeDelta * 0.5)

let metersInLatitude = loc1.distance(from: loc2)
let metersInLongitude = loc3.distance(from: loc4)

let center:CLLocationCoordinate2D = (MyCustomAnnotation?.coordinate)!
let coordinateRegion = MKCoordinateRegionMakeWithDistance(center, metersInLatitude, metersInLongitude)
mapView.setRegion(coordinateRegion, animated: true)

而且效果很好 - 每次当用户点击任何 MyCustomAnnotation 时,它都会在保持当前缩放级别的同时将 map 居中。

我想稍微更改此代码,以便仅当该点靠近屏幕的四个边缘之一时,它才会将 map 居中放置在该点上。当注释点靠近屏幕中心时——我们不应该将 map 居中。我该怎么做?我考虑过(以某种方式)检查 MyCustomAnnotation?.coordinate 和由这四个点 loc1loc2 设置的区域之间的差异loc3, loc4, 但我不知道如何处理。你能帮我吗?

最佳答案

我不会将您的注释位置与所有四个边缘进行比较,但会检查距中心位置的距离。

let distance = center.distanceFromLocation(annotationLocation)

如果此距离大于某个半径,则注释靠近其中一条边。

if distance > radius {
//center annotation
}

可以根据区域跨度计算半径。

let radius = min(region.span.longitudeDelta, region.span.latitudeDelta) * 0.3

关于ios - 仅本地图靠近屏幕边缘时,如何才能将 map 置于注释 View 的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40206211/

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