Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios MapKit视图未放大到用户位置_Ios_Swift - Fatal编程技术网

Ios MapKit视图未放大到用户位置

Ios MapKit视图未放大到用户位置,ios,swift,Ios,Swift,在我的应用程序中,我有一个MKMapKit视图,当我的应用程序启动时,如果用户允许定位服务,我希望地图能够放大到用户的位置。我写的代码是: override func viewDidLoad() { super.viewDidLoad() mapKitView.delegate = self mapKitView.showsUserLocation = true locationManager.desiredAccuracy = kCLLocationAccur

在我的应用程序中,我有一个
MKMapKit
视图,当我的应用程序启动时,如果用户允许定位服务,我希望地图能够放大到用户的位置。我写的代码是:

override func viewDidLoad() {
    super.viewDidLoad()

    mapKitView.delegate = self
    mapKitView.showsUserLocation = true
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.delegate = self

    if (CLLocationManager.locationServicesEnabled()) {

        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
    }

    locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.startUpdatingLocation()
    }

    DispatchQueue.main.async {
        self.locationManager.startUpdatingLocation()
    }
}


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

    let noLocation = CLLocationCoordinate2D()
    let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 200, 200)
    mapKitView.setRegion(viewRegion, animated: false)
}

在我的应用程序中,它显示用户的位置,但不设置动画和放大。

使用用户的位置计算视图区域,并将
设置动画的
参数更改为
true

if let location = mapKitView.userLocation.location {
    let viewRegion = MKCoordinateRegionMakeWithDistance(location, 200, 200)
    mapKitView.setRegion(viewRegion, animated: true)
}

使用用户的位置计算视图区域,并将
动画
参数更改为

if let location = mapKitView.userLocation.location {
    let viewRegion = MKCoordinateRegionMakeWithDistance(location, 200, 200)
    mapKitView.setRegion(viewRegion, animated: true)
}

在Swift 3.0中如果要在地图视图的起始视图上缩放地图套件,应使用此代码

var zoomIn = false
var zoomAnnotation:MKAnnotation

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    if let annotation = zoomAnnotation where zoomIn == true {
           let region = MKCoordinateRegion(center: zoomAnnotation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.075, longitudeDelta: 0.075))
           mapView.setRegion(region, animated: true)
           zoomIn = false
    }
}

在Swift 3.0中如果要在地图视图的起始视图上缩放地图套件,应使用此代码

var zoomIn = false
var zoomAnnotation:MKAnnotation

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    if let annotation = zoomAnnotation where zoomIn == true {
           let region = MKCoordinateRegion(center: zoomAnnotation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.075, longitudeDelta: 0.075))
           mapView.setRegion(region, animated: true)
           zoomIn = false
    }
}

在Swift 3.0中,在didUpdateLocation方法中尝试我的代码

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        //get location cordinate
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
        print(locValue)
        //set updating location stop
        locationManager.stopUpdatingLocation()

        let location = locations.last! as CLLocation//create object of CLLocation
        //get location cordinate

        let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)

        //set map zooming using region
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
        self.mapView.setRegion(region, animated: true)
}

我希望在Swift 3.0中@Rahul Bir能起作用,请用您的didUpdateLocation方法尝试我的代码

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        //get location cordinate
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
        print(locValue)
        //set updating location stop
        locationManager.stopUpdatingLocation()

        let location = locations.last! as CLLocation//create object of CLLocation
        //get location cordinate

        let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)

        //set map zooming using region
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
        self.mapView.setRegion(region, animated: true)
}
我希望@Rahul Bir

检查这个答案可能会帮助你检查这个答案可能会帮助你