Reputation: 3094
How to implement Geo Fence with iPhone.
Is any library available to implement ?
I want to know whether iPhone is inside the Polygon or Circle defined by server or OUTSIDE so if the device is out side the specified range then it should tell the server.
How to perform such task.
Thanks in advance
Upvotes: 1
Views: 1450
Reputation: 3094
Yeapy ,
I have got the solution
Here is the sample code for the same. It shows the region and the methods implementation how to find the in - out from the region.
Upvotes: 1
Reputation: 9212
There are a number of ways to calculate whether your inside a polygon or outside. The easiest is to use the ray casting method as detailed in this function by W. Randolph Franklin:
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
{
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
For testing agsint inside or outside the circle, just calculate your distance to the center of the circle and determine if it's shorter then the radius if it.
Upvotes: 2
Reputation: 3945
You may want to use CLLocationManager. Please refer to the CLLocationManagerDelegate. There are two methods here.
- (void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region;
- (void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLRegion *)region;
Upvotes: 3