gpt4 book ai didi

ios - 如何在 swift 的 map View 上显示位置?我相信我的代码是最新的,但模拟器不显示位置或蓝点?

转载 作者:搜寻专家 更新时间:2023-10-31 22:24:12 34 4
gpt4 key购买 nike

我正在尝试使用 swift 上的 map View 在 map 上显示我的当前位置和一个蓝点。但是它没有显示我的位置,也没有显示我的代码的蓝点,但我无法显示它!可能是设置的问题?

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController {

@IBOutlet weak var mapView: MKMapView!

let locationManager = CLLocationManager()
let regionInMeters: Double = 1000

override func viewDidLoad() {
super.viewDidLoad()
checkLocationServices()
}


func setupLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}


func centerViewOnUserLocation() {
if let location = locationManager.location?.coordinate {
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
mapView.setRegion(region, animated: true)
}
}
func checkLocationServices() {
if CLLocationManager.locationServicesEnabled() {
setupLocationManager()
checkLocationAuthorrization()
} else {
// Show alert letting the user know they have to turn this on.
}
}


func checkLocationAuthorrization() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse:
mapView.showsUserLocation = true
centerViewOnUserLocation()
locationManager.startUpdatingLocation()
break
case .denied:
// Show alret instructing them how to turn on permissions
break
case .notDetermined:
break
case .restricted:
// Show alret letting them know what's up
break
case .authorizedAlways:
break
}
}
}




extension ViewController: CLLocationManagerDelegate {

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion.init(center: center, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
mapView.setRegion(region, animated: true)
}


func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
checkLocationAuthorrization()
}
}

最佳答案

您必须在 Info.plist 文件中添加以下权限

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Usage Description</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>Usage Description</string>

<key>NSLocationWhenInUseUsageDescription</key>
<string>Usage Description</string>

导入库:

import MapKit
import CoreLocation

设置委托(delegate):

CLLocationManagerDelegate,MKMapViewDelegate

添加变量:

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

viewDidLoad() 上编写以下代码:

    mapView.delegate = self
mapView.showsUserLocation = true
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest

// Check for Location Services
if CLLocationManager.locationServicesEnabled() {
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}

为位置编写委托(delegate)方法:

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

就是这样,现在您可以看到您当前的位置和蓝点。

关于ios - 如何在 swift 的 map View 上显示位置?我相信我的代码是最新的,但模拟器不显示位置或蓝点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55823223/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com