1
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
}
0
Try with MKMapViewDelegate func:
尝试使用MKMapViewDelegate func:
var isInitiallyZoomedToUserLocation: Bool = false
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
if !isInitiallyZoomedToUserLocation {
isInitiallyZoomedToUserLocation = true
mapView.showAnnotations([userLocation], animated: true)
}
}
0
In swift 4.1. To change the Zoom level you need to change the span value i.e MKCoordinateSpan(latitudeDelta: 0.95, longitudeDelta: 0.95)
在swift 4.1中。要更改缩放级别,您需要更改跨度值,即MKCoordinateSpan(latitudeDelta:0.95,longitudeDelta:0.95)
let lat = "33.847105"
let lOng= "-118.2673272"
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: Double(lat)!, longitude: Double(long)!), span: MKCoordinateSpan(latitudeDelta: 0.95, longitudeDelta: 0.95))
DispatchQueue.main.async {
self.mapView(self.mapView, regionDidChangeAnimated: true)
}