我遵循一个在线教程来制作一个简单的项目,在地图上显示用户的位置。但是,当我运行它时,不会显示用户位置,也不会弹出请求位置权限。我不知道我怎么能解决这个问题。我已经把密码附在下面。
import MapKit
import SwiftUI
struct ContentView: View {
@StateObject private var viewModel=ContentViewModel()
var body: some View {
Map(coordinateRegion: $viewModel.region)
.ignoresSafeArea()
.onAppear{viewModel.checkIfLocationServicesIsEnabled()}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
final class ContentViewModel: NSObject, ObservableObject, CLLocationManagerDelegate{
@Published var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude:37.3331516, longitude: -121.891054), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1));
var locationManager: CLLocationManager?
func checkIfLocationServicesIsEnabled(){
if CLLocationManager.locationServicesEnabled(){
locationManager=CLLocationManager()
locationManager!.delegate=self
}
else{
print("Turn location on")
}
}
private func checkLocationAuthorization(){
guard let locationManager = locationManager else {
return
}
switch locationManager.authorizationStatus{
case.notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted:
print("Your location is restricted likely due to parental controls.")
case .denied:
print("You have denied this app location permission. Go into settings to change it.")
case .authorizedAlways:
region=MKCoordinateRegion(center: locationManager.location!.coordinate, span:MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
case .authorizedWhenInUse:
region=MKCoordinateRegion(center: locationManager.location!.coordinate, span:MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
@unknown default:
break
}
func locationManagerDidChangeAuthorization(_manager: CLLocationManager) {
checkLocationAuthorization()
}
}
}发布于 2022-01-27 09:41:33
您的代码有两个问题
您不是要求permissions.
这应该是可行的:
final class ContentViewModel: NSObject, ObservableObject, CLLocationManagerDelegate{
@Published var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude:37.3331516, longitude: -121.891054), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1));
var locationManager: CLLocationManager?
func checkIfLocationServicesIsEnabled(){
if CLLocationManager.locationServicesEnabled(){
locationManager=CLLocationManager()
locationManager?.delegate=self
//ask for permissions here
//depending on your usage you could also ask for
//locationManager?.requestWhenInUseAuthorization()
locationManager?.requestAlwaysAuthorization()
}
else{
print("Turn location on")
}
}
private func checkLocationAuthorization(){
guard let locationManager = locationManager else {
return
}
switch locationManager.authorizationStatus{
case.notDetermined:
//the function containing this will only be called if the permission get requested so remove it
//locationManager.requestWhenInUseAuthorization()
case .restricted:
print("Your location is restricted likely due to parental controls.")
case .denied:
print("You have denied this app location permission. Go into settings to change it.")
case .authorizedAlways:
region=MKCoordinateRegion(center: locationManager.location!.coordinate, span:MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
case .authorizedWhenInUse:
region=MKCoordinateRegion(center: locationManager.location!.coordinate, span:MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
@unknown default:
break
}
}
// this function was wrapped inside your checkLocationAuthorization() function
//move it to outer scope. It gets called every time you request permissions
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
checkLocationAuthorization()
}
}请注意,您将不得不添加多个隐私密钥,以使这一工作。
https://stackoverflow.com/questions/70872834
复制相似问题