我在项目开始时就在用户位置上工作mapview,但现在它不会在启动时放大用户。知道怎么解决这个问题吗?我使用的代码:
在课堂上 class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate
var Locationmanager = CLLocationManager()
在viewdidload self.Locationmanager.delegate = self
self.Locationmanager.desiredAccuracy = kCLLocationAccuracyBest
self.Locationmanager.requestWhenInUseAuthorization()
self.Locationmanager.startUpdatingLocation()
self.Mapview.showsUserLocation = true
Mapview.setCenterCoordinate(Mapview.userLocation.coordinate, animated: true)
在功能上 func locationManager(manager: CLLocationManager, didUpdateLocations locations:[MKUserLocation]) {
let latitude = Locationmanager.location?.coordinate.latitude
let longitude = Locationmanager.location?.coordinate.longitude
let center = CLLocationCoordinate2D (latitude: latitude!, longitude: longitude!)
let region = MKCoordinateRegion (center: center, span: MKCoordinateSpan (latitudeDelta: 0.02, longitudeDelta: 0.02))
self.Mapview.setRegion(region, animated: true)
self.Locationmanager.stopUpdatingLocation()
和info.plist
NSLocationWhenInUseUsageDescription
如果您的目标是让地图视图显示用户的位置,那么就没有必要也没有意义告诉位置管理器做任何事情。去掉所有的代码——它实际上对你有害。只需将地图视图的userTrackingMode
设置为.follow
,然后退后。