Swiftui 使用Swift UI将CLLocationManager的代表放置在何处?
多年来我一直在使用Swift,所以,如果我做了一些愚蠢的事情,对不起,Swift用户界面也帮不了我的忙。我在底部有我正在使用的代码 因此,我正在使用SwiftUI制作一个地理围栏应用程序,我已经准备好了基础知识并开始运行,但仍有一点需要改进Swiftui 使用Swift UI将CLLocationManager的代表放置在何处?,swiftui,cllocationmanager,swift5,geofencing,Swiftui,Cllocationmanager,Swift5,Geofencing,多年来我一直在使用Swift,所以,如果我做了一些愚蠢的事情,对不起,Swift用户界面也帮不了我的忙。我在底部有我正在使用的代码 因此,我正在使用SwiftUI制作一个地理围栏应用程序,我已经准备好了基础知识并开始运行,但仍有一点需要改进 我在UserData环境变量中有GeoFenging坐标和其他内容,我从其他地方的json中得到了这些信息 拥有一个SwiftUI对象,该对象具有与当前位置的映射,并初始化CLLocationManager以处理地理围栏 我正试图在同一个文件上通过 lo
- 我在UserData环境变量中有GeoFenging坐标和其他内容,我从其他地方的json中得到了这些信息
- 拥有一个SwiftUI对象,该对象具有与当前位置的映射,并初始化CLLocationManager以处理地理围栏
locationManager.delegate = self
在setupManager()中
但这会导致
Cannot assign value of type 'GeofencingView' to type 'CLLocationManagerDelegate?'
目前,缺乏特定于SwiftUI的在线信息似乎给我造成了最大的困惑,我想我应该做的是
import SwiftUI
import MapKit
import CoreLocation
struct GeofencingView: UIViewRepresentable {
@EnvironmentObject var userData: UserData
var notification = LocalNotificationManager()
var locationManager = CLLocationManager()
func setupManager() {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
self.startGoefenceMonitoring()
}
func startGoefenceMonitoring() {
print("MapListView::startGeofenceMonitoring")
for landmark in self.userData.landmarks {
let moniteringCordinate = CLLocationCoordinate2DMake(landmark.locationCoordinate.longitude, landmark.locationCoordinate.latitude)
let moniteringRegion = CLCircularRegion.init(center: moniteringCordinate, radius: 20.0, identifier: "\(landmark.id)" )
locationManager.startMonitoring(for: moniteringRegion)
}
}
func makeUIView(context: Context) -> MKMapView {
setupManager()
let mapView = MKMapView(frame: UIScreen.main.bounds)
let count = self.userData.landmarks.count
var annotationArray: [MKAnnotation] = []
var num:Int = 0
mapView.showsUserLocation = true
mapView.userTrackingMode = .follow
return mapView
}
func updateUIView(_ uiView: MKMapView, context: Context) {
}
}
struct GeofencingView_Preview: PreviewProvider {
static var previews: some View {
GeofencingView()
.environmentObject(UserData())
}
}
UIViewRepresentable
具有类协调器
模式,可以用作委托。有关用法示例,请参见主题。谢谢,这正是我想要的。UIViewRepresentable
具有类协调器
模式,可以用作委托。有关用法的示例,请参见主题。谢谢,这正是我想要的。