首页 > 解决方案 > 使用户位置在地图上可见不起作用

问题描述

如果我正在运行我的代码,圆形的蓝点不会显示在地图上。地图上可见的用户位置不起作用。我找不到那个问题。谁能找到问题?

import UIKit
import MapKit
import CoreLocation

class GPSNewBin: UIViewController, CLLocationManagerDelegate {


    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var zurueckButton: UIButton!

    var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Ask for Authorisation from the User.
        self.locationManager.requestAlwaysAuthorization()

        // For use in foreground
        self.locationManager.requestWhenInUseAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()
        }
    }


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        //guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
        let locValue: CLLocationCoordinate2D = manager.location!.coordinate
        print("locations = \(locValue.latitude) \(locValue.longitude)")
        let userLocation = locations.last
        let viewRegion = MKCoordinateRegion(center: (userLocation?.coordinate)!, latitudinalMeters: 600, longitudinalMeters: 600)
        self.mapView.setRegion(viewRegion, animated: true)
    }

}

非常感谢 :)

标签: iosswiftcore-location

解决方案


您可以从情节提要中启用用户位置:

地图视图

或者

您也可以使用代码启用它:

mapView.showsUserLocation = true

推荐阅读