Swift- MKMapkit 只能查看一个城市?
Swift- MKMapkit view only one city?
我正在尝试制作一个应用程序来查看我要去的大学,但我无法只查看一个城市。我试图确保用户无法滚动经过城市。然后我试图覆盖那个区域。我认为 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 Link 已死。
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
}
}
我正在尝试制作一个应用程序来查看我要去的大学,但我无法只查看一个城市。我试图确保用户无法滚动经过城市。然后我试图覆盖那个区域。我认为 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 Link 已死。
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
}
}