Swift Mapkit - 远离用户位置
Posted
技术标签:
【中文标题】Swift Mapkit - 远离用户位置【英文标题】:Swift Mapkit - Move Away From User Location 【发布时间】:2015-02-10 19:44:01 【问题描述】:我想显示用户位置和周围区域,但我也想允许用户在该区域周围平移。现在,如果我尝试在地图上的其他位置滚动,它会自动将我带回以用户为中心的基本区域。我该如何阻止这个?我想在中心显示用户的初始视图,但我也希望能够滚动。在此先感谢你们,你们太有帮助了!
导入 UIKit 导入 MapKit 导入核心位置
类 ViewControllerMain: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate
@IBOutlet weak var mapView: MKMapView!
var locationManager:CLLocationManager!
override func viewDidLoad()
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.startUpdatingLocation()
mapView.showsUserLocation = true
mapView.delegate = self
let longPress = UILongPressGestureRecognizer(target: self, action: "action:")
longPress.minimumPressDuration = 1.0
mapView.addGestureRecognizer(longPress)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
let regionToZoom = MKCoordinateRegionMake(manager.location.coordinate, MKCoordinateSpanMake(0.01, 0.01))
mapView.setRegion(regionToZoom, animated: true)
【问题讨论】:
【参考方案1】:您在 didUpdateLocations 中的代码正在重置该区域。你有两个选择。
无论您是否已设置第一个位置,都存储在 ivar 中。只有当你没有这样做时,你才设置区域。
设置一个运行 15 秒的计时器。如果地图被用户移动,则重置计时器。当计时器到期时,您可以重新定位到用户位置。
这将保持地图以用户为中心,但将使他们能够平移一点以获取一些上下文。
This answer shows how to do it in Objective-C
【讨论】:
你能提供一个例子吗?我理解这个概念。从语法上讲,我做错了什么【参考方案2】:locationManager.stopUpdatingLocation();
这就是你在 locationManager 结束时所需要的一切,如果你还在寻找
【讨论】:
【参考方案3】:This thread had a good example 在 Swift 和 Obj C 中。如果您使用 Swift,请务必查看我链接到的答案的评论。
设置完成后,在 didUpdateLocations 中使用控制流,以便仅在用户未触摸地图时才重新居中用户的位置。
以我的完整代码为例:
@IBOutlet weak var theMap: MKMapView!
// ...
// This var and the three following functions are used to tell if the map moves because of the user.
// This is used in the control flow in didUpdateLocations
private var mapChangedFromUserInteraction = false
private func mapViewRegionDidChangeFromUserInteraction() -> Bool
let view: UIView = self.theMap.subviews[0] as UIView
// Look through gesture recognizers to determine whether this region change is from user interaction
if let gestureRecognizers = view.gestureRecognizers
for recognizer in gestureRecognizers
if( recognizer.state == UIGestureRecognizerState.Began || recognizer.state == UIGestureRecognizerState.Ended )
return true
return false
func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool)
mapChangedFromUserInteraction = mapViewRegionDidChangeFromUserInteraction()
if (mapChangedFromUserInteraction)
// user changed map region
println("user changed map region")
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool)
if (mapChangedFromUserInteraction)
// user changed map region
println("user changed map region")
// This function is called each time the user moves.
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
// Use Control Flow: if the user has moved the map, then don't re-center.
// NOTE: this is using 'mapChangedFromUserInteraction' from above.
if mapChangedFromUserInteraction == true
// do nothing, because the user has moved the map.
else
// update on location to re-center on the user.
// set X and Y distances for the span (zoom). This is very zoomed in.
let spanX = 0.0005
let spanY = 0.0005
// Create a region using the user's location, and the zoo.
var newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
// set the map to the new region
theMap.setRegion(newRegion, animated: true)
【讨论】:
以上是关于Swift Mapkit - 远离用户位置的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Swift 中使用 MapKit 在两个位置之间绘制路线?
如何使用 mapkit 和 swift 在设定的位置覆盖一个圆圈
使用 mapKit 的地图显示相反的方向(iOS Swift)