2015-02-10 47 views
5

我想显示用户位置和周围区域,但我也希望允许用户在该区域周围平移。现在,如果我尝试在地图上的其他地方滚动,它会自动将我带回中心用户所在的基本区域。我该如何阻止?我想在中心显示用户的初始视图,但我也希望能够滚动。在此先感谢你们,如此有帮助!Swift Mapkit - 远离用户位置

进口的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) 
} 

回答

4

你在didUpdateLocations代码复位区域。你有两个选择。

  1. 存放在伊娃,你是否已经设置了第一的位置。只有当你不这样做,然后设置区域。

  2. 设置运行15秒的计时器。如果地图由用户移动,则重置计时器。当计时器到期时,您可以重新接入用户位置。

这将保持地图围绕用户居中,但将使他们能够平移一点以获得一些上下文。

This answer shows how to do it in Objective-C

+0

你能提供任一选项的例子吗?我理解这个概念。从句法上来说,我正在做一些不正确的事情 – Murph 2015-02-17 15:37:49

2
locationManager.stopUpdatingLocation(); 

这是所有你需要在的LocationManager乐趣结束,如果你仍然在这两个雨燕的OBJ C.寻找

0

This thread had a good example一定要寻找在评论答案我已经链接到如果你使用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) 

     } 

}