如何在地图上显示当前位置?

Posted

技术标签:

【中文标题】如何在地图上显示当前位置?【英文标题】:How do i show current location on map? 【发布时间】:2020-03-04 19:31:52 【问题描述】:

在我的代码中,我试图访问用户的当前位置并将其显示在地图上。我目前遇到致命错误Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value 错误在ViewDidLoad 内部。这样做的正确方法是什么?我有所有 3 个正确的隐私位置 *总是在使用时,在使用时使用说明,总是使用说明 *

    import UIKit
    import MapKit
    import CoreLocation

    class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate 

        private var locationManager: CLLocationManager!
        private var currentLocation: CLLocation?

        //let locationManager = CLLocationManager()
        let mapView = MKMapView()
        //var currentLocation: CLLocation!

        override func viewDidLoad() 
            super.viewDidLoad()



            if CLLocationManager.locationServicesEnabled()
                locationManager.requestAlwaysAuthorization()//This where the fatal error appears

                locationManager.requestWhenInUseAuthorization()////This where the fatal error appears

                locationManager = CLLocationManager()
                locationManager.delegate = self
                locationManager.desiredAccuracy = kCLLocationAccuracyBest
                locationManager.startUpdatingLocation()
            
            mapView.delegate = self
            mapView.mapType = .standard
            mapView.isZoomEnabled = true
            mapView.isScrollEnabled = true

            let leftMargin:CGFloat = 10
            let topMargin:CGFloat = 60
            let mapWidth:CGFloat = view.frame.size.width
            let mapHeight:CGFloat = view.frame.size.width

            mapView.frame = CGRect(x: leftMargin, y: topMargin, width: mapWidth, height: mapHeight)

            view.addSubview(mapView)


            if let coor = mapView.userLocation.location?.coordinate
                mapView.setCenter(coor, animated: true)
            


        

        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
            defer  currentLocation = locations.last 

            if currentLocation == nil 
                // Zoom to user location
                if let userLocation = locations.last 
                    let viewRegion = MKCoordinateRegion(center: userLocation.coordinate, latitudinalMeters: 2000, longitudinalMeters: 2000)
                    mapView.setRegion(viewRegion, animated: false)
                
            

        
        func checkLocationAuthorization(authorizationStatus: CLAuthorizationStatus? = nil) 
            switch (authorizationStatus ?? CLLocationManager.authorizationStatus()) 
            case .authorizedAlways, .authorizedWhenInUse:
                locationManager.startUpdatingLocation()
                mapView.showsUserLocation = true
            case .restricted, .denied:
                // show alert instructing how to turn on permissions
                print("Location Servies: Denied / Restricted")
            case .notDetermined:
                locationManager.requestWhenInUseAuthorization()
            
        




    

【问题讨论】:

【参考方案1】:

在创建实例之前将requestAlwaysAuthorizationrequestWhenInUseAuthorization 分配给locationManager 会使应用程序崩溃。

更新如下代码,

if CLLocationManager.locationServicesEnabled()

            locationManager = CLLocationManager()
            locationManager.requestAlwaysAuthorization()
            locationManager.requestWhenInUseAuthorization()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.startUpdatingLocation()
        

【讨论】:

你知道我怎样才能让mapView 填满整个屏幕吗? 你想显示在状态栏和导航栏的上方吗?如果是,请在此处查看我对整个屏幕的回答。 ***.com/a/60532597/1244403

以上是关于如何在地图上显示当前位置?的主要内容,如果未能解决你的问题,请参考以下文章

windows phone 8 - 如何在地图上显示经度和纬度坐标(当前位置)

如何在 Android Marshmallow 上的 Google 地图上显示当前位置?

ios:mapkit如何在地图上启用当前位置按钮?

应用程序启动地图活动后,如何在 android 的地图中显示我的当前位置

如何在不刷新整个地图的情况下更新谷歌地图上的标记位置?

如何在地图片段活动上显示按钮