【问题标题】:iOS swift. MKMap, user location stuck on right side of screeniOS 迅捷。 MKMap,用户位置卡在屏幕右侧
【发布时间】:2016-06-06 06:20:46
【问题描述】:

我在view controller 上有一个mapKit。我希望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()

}

亲切的问候

韦恩

【问题讨论】:

  • 您是否设置了地图视图约束?听起来像是在屏幕外的一半
  • 谢谢,我不敢相信我忘记了这样的事情。问题解决了。

标签: ios swift location mapkit


【解决方案1】:

确保您已为地图视图设置了约束,否则它将有一些默认大小和位置,很可能会在屏幕外

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 2012-04-02
      • 1970-01-01
      相关资源
      最近更新 更多