我正在尝试显示用户的当前位置,并将区域设置为其当前位置。
这是我的代码。
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
let userLocation = locations.last
let viewRegion = MKCoordinateRegionMakeWithDistance((userLocation?.coordinate)!, 600, 600)
self.mapView.setRegion(viewRegion, animated: true)
}
会发生什么:
什么不起作用:
如果您知道我需要做些什么才能使“蓝色”当前位置指示器出现,将不胜感激。
答案 0 :(得分:0)
您需要设置
mapView.showsUserLocation = true
在viewDidLoad