MapKit zoom to user current location

后端 未结 8 516
慢半拍i
慢半拍i 2021-02-01 17:59

I am trying to simply show user\'s location on the map, but I need to when app launches, the map should zoom to current location ,but I don\'t know why map doesn\'t zoom at all

8条回答
  •  野的像风
    2021-02-01 18:22

    when you set region -> you cannot zoom the map anymore. below to fix that

    func yourFuncName() {
    //this is global var
    regionHasBeenCentered = false
    if !self.regionHasBeenCentered {
        let span: MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
        let userLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(_cllocationOfUserCurrentLocation!.coordinate.latitude, _cllocationOfUserCurrentLocation!.coordinate.longitude)
        let region: MKCoordinateRegion = MKCoordinateRegionMake(userLocation, span)
    
        self.mapView.setRegion(region, animated: true)
        self.regionHasBeenCentered = true
        }
    
        self.mapView.showsUserLocation = true
    }
    

提交回复
热议问题