我想显示用户位置和周围区域,但我也希望允许用户在该区域内平移。现在,如果我尝试在地图上的其他位置滚动,它会自动将我带回到基本区域,用户位于中心。我怎么阻止这个?我想在中心显示用户的初始视图,但我希望能够滚动。在此先感谢你们,我们非常乐于助人!
导入UIKit 导入MapKit 导入CoreLocation
类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)
}
答案 0 :(得分:5)
您在didUpdateLocations中的代码正在重置该区域。你有两个选择。
存放在ivar中,无论您是否已设置第一个位置。只有你没有做,然后设置区域。
设置一个运行15秒的计时器。如果用户移动了地图,则重置计时器。当计时器到期时,您可以重新定位到用户位置。
这将使地图以用户为中心,但会让他们平移一点以获得一些背景。
答案 1 :(得分:2)
locationManager.stopUpdatingLocation();
如果您还在寻找
,这就是您在locationManager乐趣结束时所需要的一切答案 2 :(得分:0)
This thread had a good example。如果你使用Swift,请务必查看我已经链接到的答案的评论。
设置完成后,在didUpdateLocations中使用Control Flow,这样只有当用户没有触摸地图时,它才会重新定位用户的位置。
以下是我的完整代码示例:
@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)
}
}