Square Region Geofencing for iOS 10 and Swift 4

An Alternative region monitoring to circular region

yves songolo
2 min readFeb 13, 2019

Geofences, geofences, geofences... what a beautiful technology. 😊😊

A little bit of definition: A geofence is a virtual perimeter for a real-world geographic area. A geofence could be dynamically generated — as in a radius around a point location, or it can be a predefined set of boundaries (such as school zones or neighborhood boundaries).

Yep, if you didn’t figure that out yet, this definition came from my buddy Wikipedia who does a great job at explaining things. (That’s my personal opinion! 😌)

Now let get into the subject!

iOS 10 did a good job with geofencing. But this came with some limitations, such as the number of regions you are allowed to monitor (limited to 20), the region’s radius (no less than 100 meters), and being restricted to using only one shape for monitoring: a perfectly circular region.

What happen if you want to monitor a square region within 40 meters of length?

Well, that was the problem I was running into for a while. To solve this problem, I had to make my own CocoaPods library “ SquareRegion” to work around this problem.

Demo

Multi location monitoring
Single location monitoring

How does SquareRegion work?

First, add SquareRegion in your podfile.

pod 'SquareRegion'

Explanation:

  1. Import SquareRegion.
  2. Setup the delegate.
  3. Assign the delegate to the View Controller (self).
  4. Set the center coordinate of your region (square region).
  5. Declare your square region property: regionWithCenter is the center of your region, sideLength is the length of your square (it is in kilometers, so you need to convert to meters), and identifier is a unique key for your region.
  6. Add the region-to-monitor by using the method addRegionToMonitor(region:).
  7. Add the CLLocationManagerDelegate of Core Location (as an extension).
  8. Start updating the square region with the new location(s).
  9. Conform the View Controller to the RegionProtocol.
  10. Do your cool thing, when entering or leaving your region with relevant delegate methods!

How to simulate geolocation on iOS?

Well, there’s a good related tutorial that you can read from Appcoda.

SquareRegion is an alternative for circular regions of Core Location. It allows you to observe as much location bounds that you want within a small region with at least 25 meters of side length.

You can upvote this project on Product Hunt as well! 👍🏾😊

You can find the pod repos here.

Add and follow me on my social media!

--

--