iOS迅速。 MKMap,用户位置卡在屏幕右侧

时间:2016-06-06 06:20:46

标签: ios swift location mapkit

mapKit上有一个view controller。我希望map向用户显示当前位置。它似乎工作但用户位置卡在屏幕的右侧。当你尝试从右边滚动它时它会弹回那里。无法理解我做错了什么?任何帮助将不胜感激。

 override func viewDidLoad() {
    super.viewDidLoad()
    clientAddressTextField.text = clientAddress
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    self.mapView.showsUserLocation = true  
 }

 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let location = locations.last
    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
    self.mapView.setRegion(region, animated: true)
    self.locationManager.stopUpdatingLocation()

}

亲切的问候

韦恩

2 个答案:

答案 0 :(得分:0)

确保您已为地图视图设置了约束,否则它将具有一些默认大小&位置,可能是屏幕外的

答案 1 :(得分:-1)

使用mapView的委托方法mapView:didUpdateUserLocation:

func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
    mapView.setRegion(MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800), animated: true)
}