我有一个标签栏,一个用于显示地图。当我切换到此标签栏时,它将请求权限并自动获取当前位置。
我完成了代码,将模拟位置设置为Apple,它会在屏幕中央显示一个蓝点,但是当我选择自定义位置时,指定一个位置,蓝点不显示在中心地图。
我需要拖动地图,并在中心位置附近找到蓝点约1~2km。我在真实设备上测试过并遇到同样的问题。
使用该方法自动显示当前位置,但不显示蓝点。
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager!.delegate = self
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse{
locationManager!.startUpdatingLocation()
locationManager!.desiredAccuracy = kCLLocationAccuracyBestForNavigation
}else{
locationManager!.requestWhenInUseAuthorization()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last!
let coordinateRegion = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 100, 100)
mapView.setRegion(mapView.regionThatFits(coordinateRegion), animated: true)
location = newLocation
locationManager?.stopUpdatingLocation()
locationManager = nil
}
我添加了一个按钮 - 当它没有显示蓝点时,单击该按钮将在地图中心设置蓝点。这两种方法具有相同的功能,但它们产生不同的视图。为什么呢?
@IBAction func showUser() {
let region = MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 100, 100)
mapView.setRegion(mapView.regionThatFits(region), animated: true)
答案 0 :(得分:0)
我只是使用此编码更新到mapview中心的位置
请将此密钥添加到info.plist文件
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
map.delegate = self
map.showsUserLocation = true
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse{
locationManager.startUpdatingLocation()
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
}else{
locationManager.requestWhenInUseAuthorization()
}
}
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
mapView .setCenter(userLocation.coordinate, animated: true)
}
您可以从HERE
下载演示版