如何在swift 3.0中使用coreLocation框架创建20多个地理围栏

时间:2017-02-01 05:05:52

标签: ios swift3 mkmapview cllocationmanager geofencing

我目前正在使用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) {

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

    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!
}