2015-02-10 77 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) 

     } 

}