xcode - 不知道如何在 SwiftUI (Xcode 12.4) 中打开地图
问题描述
我刚开始在 Xcode 中编程,有些事情对我来说似乎还是有点难以理解。我在 medium.com 上找到了一个带有代码的教程,以使用 MapKit 实现当前用户位置。获取用户位置并将其显示在地图上的代码写在 MapViewController.swift 文件和类中,如下所示。
import Foundation
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, CLLocationManagerDelegate {
let mapView = MKMapView()
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
setupMapView()
checkLocationServices()
// Do any additional setup after loading the view.
}
func setupMapView() {
view.addSubview(mapView)
mapView.translatesAutoresizingMaskIntoConstraints = false
mapView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
mapView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
mapView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
let region = MKCoordinateRegion.init(center: location.coordinate, latitudinalMeters: 4000, longitudinalMeters: 4000)
mapView.setRegion(region, animated: true)
}
func checkLocalAuthorization() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse:
mapView.showsUserLocation = true
followUserLocation()
locationManager.startUpdatingLocation()
break
case .denied:
// Show alert
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted:
// Show alert
break
case .authorizedAlways:
break
@unknown default:
fatalError()
}
}
func checkLocationServices() {
if CLLocationManager.locationServicesEnabled() {
setupLocationManager()
checkLocalAuthorization()
} else {
// user did not turn it on
}
}
func followUserLocation() {
if let location = locationManager.location?.coordinate {
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: 4000, longitudinalMeters: 4000)
mapView.setRegion(region, animated: true)
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
checkLocalAuthorization()
}
func setupLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
}
问题是我不知道如何运行代码以使地图可见。当我在 ContentView.swift 中调用 MapViewController() 类时,如下所示,我收到错误“属性'body'的返回类型要求'MapViewController'符合'View'”和“静态方法'buildBlock'需要'MapViewController'符合‘观点’”。有人可以解释我如何修复错误并只查看地图吗?非常感谢提前
import SwiftUI
import UIKit
import CoreLocation
import MapKit
struct ContentView: View {
var body: some View {
MapViewController()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
解决方案
您将需要一个 UIViewControllerRepresentable:
struct MapViewRepresentable: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> some UIViewController {
return MapViewController()
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
}
}
struct ContentView: View {
var body: some View {
MapViewRepresentable() //<< here use your UIViewControllerRepresentable
}
}
推荐阅读
- c++ - 在 C++ 中定义跨多个文件共享的全局常量的最佳方法
- javascript - ZenDesk API 无法在对象内发布数组
- python - 通过 Pandas DataFrame 行循环函数
- powershell - Powerhsell/Unix Bash - 查找一组文件的缺失数字
- angular - 前端应该如何处理 OAuth Scopes
- javascript - 为什么通过 map 运行时使用扩展运算符的数组副本会修改原始数组?
- php - zsh:找不到命令:带有 MAMP 的 mysql
- laravel - Laravel - 如何让 Cron Job 在我的本地主机 Windows 上工作
- nginx - Nginx 重定向,将 self 作为引用者
- c# - 从json字符串中删除一个字段