【发布时间】: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)
}
【问题讨论】: