ユーザーの場所をマップに表示するだけですが、アプリの起動時にマップを現在の場所にズームする必要がありますが、マップがズームしない理由はわかりませんすべてのそれはこのようなものだ:ここでMapKitユーザーの現在地にズームする
はコードです:
class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.showsUserLocation = true
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
DispatchQueue.main.async {
self.locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations.last as! CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
var region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
region.center = mapView.userLocation.coordinate
mapView.setRegion(region, animated: true)
}
削除 'region.center = mapView.userLocation.coordinate' – shallowThought
@shallowThought私がしたけど何もlocationManagerは、それが呼び出された最初の時間が初期化されていないので、このコードスニペットが失敗した –