GeoFencing & Polygons

Hello Community. This is my first post. I am working with an app developer and we hope to have submitted to the store by January.


We have run into a roadblock and I was hoping someone here could shed some light on the subject and help us accomplish our goals. I apologize in advance if I dont use all of the correct terminology as I have only minor knowlede in the way apps are created.


One of the main functionalites of my app is that you recieve a notification when you are near a state border in the United States. (for instance x amount of miles from the border between oregon and washington). Eventually we got to the point where we had to determine where the state borders actually were. After some research i provided him with the information located here: http://*******.com/pxglu67and instructed him to use that information to define where the broders are, and hence when the notifications will appear. On this page are KML files which contain the actual locations of the state borders according to general knowledge.


His reply was: "we do have a issue with polygon fencing on map in ios. we can only fencing with radius not a polygon." I do remember hearing this and I believe him, as he has proven himself skilled in many other ways.


My first question is; Is this true that apple map kit can not be used in this manner? And that there is no alternative way to accomplish this?


If so, Is there a way to accomplish this with a third party map system like google. or by sharing the location to a server which would then, push a notification?


How would the community tackle this problem?


Any insight, no matter how simple, is greatly appreciated!


Mike

Replies

I believe your developer is correct that there's no API that will wake the app upon entry/exit of an odd shaped polygon.


I would probably try a fairly simple algorithm first - calculate the distance to the nearest border. Set up your circular region from your current location based on that distance. When the app is woken due to exiting that circle, check again. If you're near the border, post your local notification. If not, repeat (find distance to nearest border, set up region etc.)


I suppose if you were traveling parallel to the border *just* outside your maximum distance that would trigger the notification, the app may be woken up more times than necessary, but in the absence of a proper API for it there may be no ideal solution.

Hi,


I thought maybe you could pepper the borders with overlapping circular regions to wake the app up when it is near a border. So what I mean is to have loads of

CLCircularRegion's with centers on the borderline then your app could wake up when in proximity to the border.


The only alternative solution I could think of is to have the app running in the background but that is never a good solution since the user might explicitly turn it off and then you are not monitoring anything.


Hope all goes well.

I see what youre saying. I could start by putting a circular region at each road crossing for instance, which would cover most traffic over the border.

With this method, would the user still be able to choose the distance for the alert oir a circular regions fixed?


As far as your alternative solution (having the app running in the background) could you elaborate on that? if i designed the program to always be running, how would this method help accomplish my goal?


Thanks in advance!


Mike

So, even if the app cant be woken in this way, can a polygon be created so that if hypothetically the user chose to have the app always running he would be able to get notifications based on the polygon? Either from the app itsself or by setting up a service to track location and then push notifications basd on that user's location?


I tried to write my response question in the most succint way, i hope you understand what i'm asking...


Mike

There is no such thing as "setting up a service to track location" if you mean some service on a server. There is only the app. Yes you can do background location tracking in the app at a major cost to battery life. It's not absolutely guaranteed that your app will stay alive forever in the background though, so there is no foolproof approach that will work 100% on iOS. Once killed your app will no longer detect location changes. The region monitoring / significant location change service would probably be the more reliable approach since it should restart your app on location change even if it was killed due to memory pressure or some other event (e.g. reboot).

Another related question for anyone who can help.


Can what I need to do be accomplished using Google Maps in my app rather than Apple Maps? Can this even still be done?


I was looking around and found this page: https://developers.google.com/maps/documentation/ios-sdk/shapes


It looks promising, but I dont want to push my developer in a totally different direction unless its worth exploring.


Does anybody have any experience with this?

This solution works -- you need three components:


Component One: detect if a location point resides within a polygon -- use this to determine which state polygon you're inside of at the moment.

1) Convert each state shape into a MKPolygon (you have KML files, SHP files are more current and easier to find) -- ("statePolygon" in the reference code below). Caveat: the polygon points must be ordered and terminate at the origin point. If the points are disordered, the solution won't work (imagine you're drawing a square on paper without lifting your pen...make sure point A connects to B, B to C, C to D and D back to A again. All shape files/KML files should be in this format but verify the data just to be sure.)

2) Convert your current CLLocation point (myLocationPoint) into a MKMapPoint (mapPoint in the code below)

3) the BOOLean return will be TRUE if the point is inside the state polygon, or on a polygon line segment


MKPolygonRenderer *statePolygonRenderer = [[MKPolygonRenderer alloc] initWithPolygon:statePolygon];

MKMapPoint testMapPoint = MKMapPointForCoordinate(myLocationPoint);

CGPoint statePolygonRenderedPoint = [statePolygonRenderer pointForMapPoint:testMapPoint];

BOOL hitTest = CGPathContainsPoint(statePolygonRenderer.path, NULL, statePolygonRenderedPoint, NO);


Component Two: detect distance to next state (requires easy math) -- use this to determine which state you're approaching

1) The code is long, so rather than rewrite it here simply search online for "shortest distance between point and finite line segment". Choose an algorithm which works for finite lines and accounts for 0 length lines (crucial). stackoverflow has lots of candidate algorithms and iOS code to choose from.

2) build a table of states along with the names of states bordering each state

3) Use component One above to determine which state you're in, and use the table of states to determine which states to check your current distance to.

4) each state's line segments (remember A to B, B to C, C to D and D to A from above...) are the lines you're checking your distance to. Set up a trigger for the distance you think is relevant by peridically recalculating the distance between the device and each neighboring state's line segments.


Component Three: efficient location management -- required to guarantee happy users and precise border detection

1) Don't use bestfornavigation type of location tracking, even while navigating in a car, and don't use geofences -- they're both battery drains, and geofencing is unreliable when precision matters.

2) Either build an energy efficient location system, or use a 3rd party location framework (I make one, there are others too) and use them to poll for location updates periodically -- user battery performance is crucial so carefully manage battery life


Good luck!

Cheers-

Phillip

Check iWatchU2 on the App Store...

See if you can use it as a proof of concept or point you to a use-case scenario.


The app is free and you can test the navigation functions, alarms with notifications, based on waypoints with circular and/or polygon shaped arrival/departure zones.

Many polygon-shaped boundaries can be used from an app repository (e.g. Countries, States, Cities, Zip Codes, user-defined, etc).


With Apples' restriction (and there are many) on third-party developers implementing true background-style programs in iOS, desirable features (such as launching a task automatically based on accurate GPS events) cannot be offered to users.

Hello All
I will create Geofence base project demo with Circular polygon please check below link :-
https://stackoverflow.com/a/65558490/14215143