2015-02-10 22 views
5

Tôi muốn hiển thị vị trí của người dùng và khu vực xung quanh, nhưng tôi cũng muốn cho phép người dùng xoay quanh khu vực. Ngay bây giờ nếu tôi cố gắng di chuyển đến một nơi khác trên bản đồ, nó sẽ tự động đưa tôi trở lại vùng cơ sở với người dùng ở trung tâm. Làm cách nào để ngăn chặn điều này? Tôi muốn hiển thị chế độ xem ban đầu với người dùng ở giữa, nhưng tôi cũng muốn có thể cuộn xung quanh. Cảm ơn trước các bạn rất hữu ích!Swift Mapkit - Di chuyển ra khỏi vị trí người dùng

nhập khẩu UIKit nhập khẩu MapKit nhập khẩu CoreLocation

lớp 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) 
} 

Trả lời

4

Mã của bạn trong didUpdateLocations được đặt lại khu vực. Bạn có hai lựa chọn.

  1. Lưu trữ trong một ngà có hay không bạn đã đặt vị trí đầu tiên. Chỉ khi bạn chưa làm thì hãy đặt khu vực.

  2. Đặt hẹn giờ chạy trong 15 giây. Nếu bản đồ được di chuyển bởi người dùng bạn đặt lại bộ hẹn giờ. Khi bộ hẹn giờ hết hạn, bạn có thể quay lại vị trí của người dùng.

Điều này sẽ giữ bản đồ tập trung xung quanh người dùng nhưng sẽ cho phép họ xoay quanh một chút để có được một số ngữ cảnh.

This answer shows how to do it in Objective-C

+0

bạn có thể cung cấp một ví dụ về một trong hai lựa chọn? Tôi hiểu khái niệm. Cú pháp tôi đang làm gì đó sai – Murph

2
locationManager.stopUpdatingLocation(); 

đây là tất cả bạn cần tại cuối locationManager vui vẻ, nếu bạn vẫn đang tìm kiếm

0

This thread had a good example trong cả hai Swift và obj C. Hãy chắc chắn để tìm kiếm những nhận xét trên Tôi đã liên kết với bạn nếu bạn sử dụng Swift.

Sau khi bạn thiết lập điều đó, hãy sử dụng Luồng điều khiển trong didUpdateLocations để nó chỉ định tâm lại vị trí của người dùng khi người dùng không chạm vào bản đồ.

Đây là mã đầy đủ của tôi là một ví dụ:

@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) 

     } 

} 
Các vấn đề liên quan