ios - 如何在swift的地图视图上显示位置?我相信我的代码是最新的,但模拟器没有显示位置或蓝点?
问题描述
我正在尝试使用 swift 上的地图视图在我当前的位置和地图上显示一个蓝点。但是它没有显示我的位置,也没有显示蓝点我对我的代码非常肯定,但我无法让它显示!应该是设置的问题吧?
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
设置代表:
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()
}
为位置编写委托方法:
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)
}
}
}
就是这样,现在您可以看到您当前的位置和蓝点。
推荐阅读
- java - 如何在 Intellij IDEA 中使用结构搜索命令查找已弃用的 java 类的用法?
- c# - 将字符串参数传递给带引号的命令文本
- mysql - 使用 Terraform 转储/恢复 MariaDB 数据库
- html - 尝试在菜单项周围添加边框并使一项具有不同的颜色
- highcharts - 我想在所有级别使用“布局:悬挂”功能,但是当我这样做时,我会得到重叠的节点和不正确的线
- javascript - Socket.io 为一个事件多次返回
- regex - 正则表达式匹配带有前缀的字符串,然后不区分大小写,然后带有后缀
- nativescript - 文件名有任何命名约定吗?
- performance - Python 请求 - 批量 API 调用
- react-native - 我们可以使用 react-native 来构建 Android Wear 应用吗?