【问题标题】:How to create more than 20 geofences using coreLocation framework in swift 3.0如何在 swift 3.0 中使用 coreLocation 框架创建 20 多个地理围栏
【发布时间】:2017-02-01 05:05:51
【问题描述】:

我目前正在使用 GeoFencing,我能够在 Apple 提供的 CoreLocation 框架的帮助下创建 GeoFence。但是当应用程序启动时,我无法创建超过 20 个栅栏。这是我的代码

提前致谢

 func createGeofence() {

     if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {



         // region data
         let title = "Verity's"
         let coordinate = CLLocationCoordinate2DMake(17.4220382049842 , 78.3794177044913)
         let regionRadius = 150.0


         // setup region

         let region = CLCircularRegion(center:CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude), radius: regionRadius, identifier: title)

         //Added monitoring
         locationManager.startMonitoring(for: region)  
     }

     else {
     }

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {

       self.enterRegion = region
}


func locationManager(_ manager: CLLocationManager, didExitRegionregion: CLRegion) {

}

【问题讨论】:

  • 您能解释更多细节以提供帮助吗?
  • 当然 prakash,我正在为这个位置创建地理围栏,让坐标 = CLLocationCoordinate2DMake(17.4220382049842 , 78.3794177044913) 有 100 个半径,但我需要创建多个地理围栏。希望能让你明白
  • 您可以为一个应用创建 20 个 goefences。因此可以检查当前正在跟踪多少地理围栏。如果超过 20,则将无法正常工作。创建多个 goefences 时遇到什么错误。
  • 我无法创建。我的意思是我不知道如何创建
  • 嘿@BoosaRamesh,请参考我的答案,我已经修复并且它对我来说工作正常。

标签: ios swift3 mkmapview cllocationmanager geofencing


【解决方案1】:
var locationManager = CLLocationManager()

@IBAction func didTapOnSaveGeoFences(_ sender: AnyObject) {
    let region = self.region(withGeotification: CLLocationCoordinate2D(latitude: (myLocation?.latitude)!, longitude: (myLocation?.longitide)!), radius: CLLocationDistance(myLocation!.distance),identifier: "\(myLocation?.locationID)", entry: true)
    locationManager.startMonitoring(for: region)
}

func region(withGeotification coordinate: CLLocationCoordinate2D, radius: CLLocationDistance, identifier: String, entry: Bool) -> CLCircularRegion {
    // 1
    let region = CLCircularRegion(center: coordinate, radius: radius, identifier: identifier)
    // 2
    region.notifyOnEntry = entry
    region.notifyOnExit = entry
    return region
}

更多详情:https://www.raywenderlich.com/136165/core-location-geofencing-tutorial

【讨论】:

    【解决方案2】:
        func determineMyCurrentLocation() {
    
            let locationModel5 = LocationModel()
            locationModel5.name = "locationModel 300"
            locationModel5.lat = 37.33448791
            locationModel5.long = -122.03941089
            locationModel5.radious = 300
    
            let locationModel4 = LocationModel()
            locationModel4.name = "locationModel 200"
            locationModel4.lat = 37.33448791
            locationModel4.long = -122.03941089
            locationModel4.radious = 200
    
            let locationModel3 = LocationModel()
            locationModel3.name = "locationModel 150"
            locationModel3.lat = 37.33290406
            locationModel3.long = -122.05391527
            locationModel3.radious = 150
    
            locationManager = CMCLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()
            locationManager.allowsBackgroundLocationUpdates = true
    
            if CLLocationManager.locationServicesEnabled() {
                locationManager.startUpdatingLocation()
            }
    
            let ary : [LocationModel] = [locationModel4,locationModel5,locationModel3]
    
            for mdl in ary {
    
                let geofenceRegionCenter = CLLocationCoordinate2DMake(mdl.lat, mdl.long);
                let geofenceRegion = CLCircularRegion(center: geofenceRegionCenter, radius: mdl.radious, identifier: mdl.name);
                geofenceRegion.notifyOnExit = true;
                geofenceRegion.notifyOnEntry = true;
    
                let span = MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)
                let mapRegion = MKCoordinateRegion(center: geofenceRegionCenter, span: span)
                self.map.setRegion(mapRegion, animated: true)
    
                let regionCircle = MKCircle(center: geofenceRegionCenter, radius: mdl.radious)
                self.map.add(regionCircle)
    
                locationManager.startMonitoring(for: geofenceRegion)
            }
        }
    
    
    //Location manage delegate method:
    
        func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    
                self.showAlert(message: ("\(region.identifier) didEnterRegion"), identifier: ENTERED_REGION_NOTIFICATION_ID)
    
        }
    
        func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
    
                self.showAlert(message: ("\(region.identifier) didExitRegion"), identifier: ENTERED_REGION_NOTIFICATION_ID)
    
        }
    
    //Show alert when entering and exiting
    
        func showAlert(message: String, identifier: String) {
    
            let alert = UIAlertController(title: "Alert", message: message, preferredStyle: .alert)
            let action = UIAlertAction(title: "OK", style: .destructive, handler: nil)
            alert.addAction(action)
            self.present(alert, animated: true, completion: nil)
        }
    
    // Use this class for temporary
    
    class LocationModel
    {
        var name:String!
        var lat:Double!
        var long:Double!
        var radious:Double!
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-23
      • 2017-02-14
      • 2016-09-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      相关资源
      最近更新 更多