gpt4 book ai didi

Swift-MKMapkit 只能查看一个城市?

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

我正在尝试制作一个应用程序来查看我要去的大学,但我无法只查看一个城市。我试图确保用户无法滚动经过城市。然后我尝试覆盖该区域。我认为 setRegion 方法将有助于解决该问题,但显然不会。关于如何设置用户不能超越的区域有什么建议吗?

    super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// sets maps to univeristy
var location = CLLocationCoordinate2DMake(42.9633,
-85.890042)
// Span and region
var span = MKCoordinateSpanMake (0.005, 0.005)
var region = MKCoordinateRegion(center: location, span: span)
Map.setRegion(region, animated: true)

最佳答案

我翻译了在这里找到的 Obj-C 代码:https://gist.github.com/Alp-Phone/e11cca67e77285566d4d到 Swift 链接已失效。

lazy var restrictedRegion: MKCoordinateRegion = {
// sets maps to univeristy
let location = CLLocationCoordinate2DMake(42.9633, -85.890042)
// Span and region
let span = MKCoordinateSpanMake (0.005, 0.005)
return MKCoordinateRegion(center: location, span: span)
}()

override func viewDidLoad() {
super.viewDidLoad()
mapView.setRegion(restrictedRegion, animated: true)
}

var manuallyChangingMap = false //Stop from updating while animating
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
if !manuallyChangingMap && ((mapView.region.span.latitudeDelta > restrictedRegion.span.latitudeDelta * 4) ||
(mapView.region.span.longitudeDelta > restrictedRegion.span.longitudeDelta * 4) ||
fabs(mapView.region.center.latitude - restrictedRegion.center.latitude) > restrictedRegion.span.latitudeDelta ||
fabs(mapView.region.center.longitude - restrictedRegion.center.longitude) > restrictedRegion.span.longitudeDelta) {

manuallyChangingMap = true
mapView.setRegion(restrictedRegion, animated: true)
manuallyChangingMap = false
}
}

关于Swift-MKMapkit 只能查看一个城市?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31799389/

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