我在项目开始时就用户位置工作了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
答案 0 :(得分:0)
如果您的目标是让地图视图显示用户的位置,则无需告知位置管理员执行任何操作,也没有必要。摆脱所有代码 - 它实际上在这里伤害你。只需将地图视图userTrackingMode
设置为.follow
即可退回。