ios - 制作当前位置。Mapkit 的 swiftui 按钮
问题描述
我使用 Mapkit 和 uirepresentable 视图制作了一个地图视图,现在我想制作一个按钮,当我按下它时,它会给出我当前的位置。我用 swiftui 制作了一个按钮,但我不知道如何在 mapview 中使用该按钮。你可以在那里看到 mapview.swift 代码。
func updateUIView(_ view: MKMapView, context: Context) {
let locationManager = CLLocationManager()
view.showsUserLocation = true
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
let locValue:CLLocationCoordinate2D = locationManager.location!.coordinate
print("CURRENT LOCATION = \(locValue.latitude) \(locValue.longitude)")
let coordinate = CLLocationCoordinate2D(
latitude: locValue.latitude, longitude: locValue.longitude)
let span = MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)
let region = MKCoordinateRegion(center: coordinate, span: span)
view.setRegion(region, animated: true)
})
}
}
解决方案
推荐阅读
- java - 握手方的递归调用
- java - 一页上实体实例的多种形式 - 未创建表单
- css - Vuetify 主题不适用于自定义主题
- c# - 来自 C# 的 LSP 悬停文本降价链接不起作用
- command-line-interface - Zapier CLI,有没有办法改变输出的显示顺序
- ubuntu - xfce4 via VcXsrv Server on wsl2 与 DPI 问题
- python - 如何在 3D 阵列的特定一侧应用 2D 蒙版?
- swiftui - SwiftUI - 将项目从一个列表拖到另一个
- python - 将 if 语句条件与用户输入语句匹配(减少多个条件)
- swift - NSEvent.addLocalMonitorForEvents 在没有窗口的应用程序开发中不起作用(构建 macOS 菜单栏应用程序)