簡體   English   中英

以用戶位置為中心的地圖未顯示藍點

[英]center the map on user's location didn't show the blue dot

我有一個標簽欄,一個用來顯示地圖。 當我切換到這個標簽欄時,它會請求權限並自動獲取當前位置。

我完成了代碼,將模擬位置設置為Apple,它會在屏幕中心顯示一個藍點,但是當我選擇自定義位置時,指定一個位置,藍點不會顯示在地圖中心。

我需要拖動地圖,會在中心位置附近找到大約 1~2 公里處的藍點。 我在真機上測試過,也有同樣的問題。

使用該方法自動顯示當前位置,但不顯示藍點。

 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)
}

截屏

截屏

我只是使用此編碼更新到地圖視圖中心的位置

請將此密鑰添加到 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)
    }

您可以從這里下載演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM