4

when I run the code below testing using my phone connected to my mac I have a GPX file and on the debugger I select "Locations" everything works fine, every region I have on the GPX file gets triggered, the notification comes up with no problem.

The issue is when I take my phone for a car ride and I pass the exactly regions I have on my GPX file , the notifications don't get triggered just the first one.

On the method

func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
        print("Hi \(region.identifier)")

}

I can see all the regions had been started for monitoring

I've been stuck on this for quite few days now... Could you please someone help me .

Code follows below.

func GetAllILocations(){
        if let url = URL(string: "http://www.goemobile.com/mobile/liquidnitro/getlocations.php"){
            var request = URLRequest(url:url)
            request.httpMethod = "POST";// Compose a query string
            let postString = ""
            request.httpBody = postString.data(using: String.Encoding.utf8)
            let task = URLSession.shared.dataTask(with:request) { data, response, error in

                if error != nil{

                    return
                }
                do {
                    if let convertedJson = try JSONSerialization.jsonObject(with: data!, options: []) as? [[String:Any]] {

                        for location in convertedJson {
                            if  ((location["locationid"] as? Int)! > 0) {
                                let latitude = location["latitude"] as! Double
                                let longitude = location["longitude"] as! Double
                                let title = location["locationtitle"] as! String
                                let subtitle = location["locationsubtitle"] as? String

                                let annotation = MKPointAnnotation()

                                annotation.title = title
                                annotation.subtitle = subtitle
                                annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
                                self.mapView.addAnnotation(annotation)
                                let center = CLLocationCoordinate2DMake(latitude, longitude)
                                let region = CLCircularRegion.init(center: center, radius: 0.5, identifier: title)
                                region.notifyOnEntry = true;
                                region.notifyOnExit = false;
                                self.locationManger.startMonitoring(for: region)

                            }
                        }
                    }
                }
                catch let error as NSError {
                    print(error.localizedDescription)
                }
            }
            task.resume()
        }
    }

    func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
        print("Hi \(region.identifier)")

    }

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        lblRegion.text = region.identifier
        if region is CLCircularRegion {
            scheduleNotification(inSeconds: 0.5, storeName:region.identifier, completion: {success in
                if !success{
                    let alert = UIAlertController(title: "Error Alert", message: region.identifier, preferredStyle: .alert)
                    let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                    alert.addAction(action)
                    self.present(alert, animated: true, completion: nil)
                }
            })
        }
    }
Rodrigo Schreiner
  • 153
  • 1
  • 1
  • 7
  • @Rodrigo did you find the solution?? Even I am stuck with the same problem now. – SaiPavanParanam Dec 20 '16 at 14:49
  • @SaiPavanParanam Did you find any solution to this problem? – fragon Dec 29 '16 at 23:36
  • yes I have found it... I was activating geoFencing based on the distance moved.. Since in the simulator we can move way faster than real world, in my code i had geofence activated only if we move more than 0.5 miles in 10 seconds. now we can't move that fast (while i was testing on my bike) i changed the code to activate geofence if was moving at least more than 0 miles(greater than zero). It was a mistake in my code – SaiPavanParanam Jan 02 '17 at 08:00
  • @SaiPavanParanam I am working on Geofence. Will u please guide me, as I have made the center having the 30-meter radius. But the functionality works for radius 100 meters. I have read that default 100 meters is taken by Apple. Is it true? As I can see you are using 0.5 meters for the region. Is it working fine for you? – Amanpreet May 07 '18 at 12:12
  • @Amanpreet as I have mentioned in the comment above, while I was testing with a gpx file in simulator, in my application there are numerous regions that I have to monitor.But we can have only 20 regions. So I activated only the nearest 20 regions and did that after moving atleast 0.5 miles from the last refresh loacation. My regions are not with radius of 0.5meters. I hope you understood what you read. – SaiPavanParanam May 10 '18 at 17:45
  • @SaiPavanParanam, I am working with only one region. I have created a region with radius 50 meters. But it is giving me entry pop up when I am 100 meters far from the location. Is it correct functionality? Where Should I expect the entry pop up will come? Will you please refer any app having geo fence functionality. It will be a great help, as I am stuck at this point. – Amanpreet May 11 '18 at 07:08

0 Answers0