【问题标题】:Could not retrieve region info - monitoring not working无法检索区域信息 - 监控不起作用
【发布时间】:2023-03-26 02:07:01
【问题描述】:

这是我的代码。一旦我在模拟器中启动应用程序。我在调试窗口中看到以下消息。

2020-05-02 23:01:37.931720-0400 SwiftMapView[42245:6851218] Metal API Validation Enabled
2020-05-02 23:01:38.216200-0400 SwiftMapView[42245:6851218] libMobileGestalt MobileGestalt.c:1647: Could not retrieve region info
import SwiftUI
import MapKit

struct MapView: UIViewRepresentable {

    var locationManager = CLLocationManager()


    func setupManager() {

        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        //locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()

        // Set Geofencing region
        //let locValue:CLLocationCoordinate2D = self.locationManager.location!.coordinate
        let locValue:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 15.833826, longitude: 78.845220)
        let geofencingRegion: CLCircularRegion = CLCircularRegion(center:locValue, radius: 100, identifier: "Crap")

        geofencingRegion.notifyOnExit = true
        geofencingRegion.notifyOnEntry = true

        // Start monitoring
        locationManager.startMonitoring(for: geofencingRegion)
    }



    func makeUIView(context: Context) -> MKMapView {
        setupManager()
        let mapView = MKMapView(frame: UIScreen.main.bounds)
        //mapView.centerCoordinate = CLLocationCoordinate2D(latitude: 15.833826, longitude: 78.845220)
        mapView.showsUserLocation = true
        mapView.userTrackingMode = .follow
        return mapView
    }


    func updateUIView(_ uiView: MKMapView, context: Context) {

        // Show current user location
        //uiView.showsUserLocation = true

        // Requst location when in use
        self.locationManager.requestAlwaysAuthorization()

        if CLLocationManager.locationServicesEnabled()
            && (CLLocationManager.authorizationStatus() == .authorizedAlways || CLLocationManager.authorizationStatus() == .authorizedWhenInUse)
            {
            //self.locationManager.delegate = self
            self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            self.locationManager.startUpdatingLocation()

            let locValue:CLLocationCoordinate2D = self.locationManager.location!.coordinate
            let coordinate = CLLocationCoordinate2D(latitude: locValue.latitude, longitude: locValue.longitude)
            let span = MKCoordinateSpan(latitudeDelta: 0, longitudeDelta: 0)
            let region = MKCoordinateRegion(center: coordinate, span: span)

            uiView.setRegion(region, animated: true)

        }
    }
}

class MapAppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

    var locationManager: CLLocationManager?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        self.locationManager = CLLocationManager()
        self.locationManager!.delegate = self

        return true
    }
}

extension MapAppDelegate{

    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        print("Hello World")
    }

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        print("Welcome Home")
    }
}

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    在模拟器中你必须添加一个位置,然后你的代码对我有用(至少如果你的 info.plist 是正确的):

    【讨论】:

      猜你喜欢
      • 2015-02-07
      • 2015-12-09
      • 2020-03-06
      • 1970-01-01
      • 2012-02-20
      • 2017-11-29
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      相关资源
      最近更新 更多