-1

I was wondering if was possible to check if a marker is outside of the region. I can check if the user left the region, but I want to also check if any marker on the map to the same thing as well, I want to check the geofence region between to markers.

  func setUpGeofence() {
        let geofenceRegionCenter = CLLocationCoordinate2DMake(getLatitude(),getLongitude());
        let geofenceRegion = CLCircularRegion(center: geofenceRegionCenter, radius: 400, identifier: "Geofence");
        geofenceRegion.notifyOnExit = true;
        geofenceRegion.notifyOnEntry = true;
        self.locationManager.startMonitoring(for: geofenceRegion)
    }
}
Gereon
  • 14,827
  • 4
  • 36
  • 62
Nouf
  • 703
  • 10
  • 26

1 Answers1

0

You can use CLLocation.distance(from:) to calculate each marker's distance from your geofence center.

let center = CLLocation(latitude: getLatitude(), longitude: getLongitude())

for marker in markers {
    if marker.location.distance(from: center) > radius {
        // outside
    } else {
        // inside
    }
}
Gereon
  • 14,827
  • 4
  • 36
  • 62
  • I dont want to check the distance I want to set region that if the car entered I know it enter and if left I know it left , if I user ".distance(from)" if my car was 5 meters way it consider it out even if did not reach the area I want to be – Nouf Dec 31 '18 at 11:17
  • So you want geofencing on multiple regions? Then just do that, iOS supports up to 20 simultaneous regions. See https://stackoverflow.com/questions/14232712/tracking-multiple-20-locations-with-ios-geofencing for ideas if you have more than 20. – Gereon Dec 31 '18 at 11:22
  • thank you for the help, but this not what I want , I dont want to know if the user enter the regions , I have GMS marker and I want to know if that marker was the one that enter the regions – Nouf Jan 01 '19 at 06:19