I want to check if anyone enter in allocated boundary then i have to alert that user like "You are entered" and when user leaves then "You left". I am using .KML file for draw boundary in which there are more than latitude and longitude. Here i attached screenshot for the same.So, my concern is that how can i detect that anyone is entered within this boundary and left from that boundary. Thank you in advance
Boundary looks like this.Red color line is boundary.

- 1,132
- 11
- 32
-
Already asked before: http://stackoverflow.com/questions/35457480/determine-if-a-gps-location-is-within-a-gpx-track-segment – Andreas Oetjen Jan 31 '17 at 06:40
-
Possible duplicate of [Determine if a GPS location is within a gpx track segment](http://stackoverflow.com/questions/35457480/determine-if-a-gps-location-is-within-a-gpx-track-segment) – Andreas Oetjen Jan 31 '17 at 06:42
-
@AndreasOetjen thank you for your comment. But it seems that the answer of the question which you mentioned is no longer available. I already saw that question but it seems that i am using .kml file and parsing using kml parser code provided by Apple not using .gpx file. – Birju Jan 31 '17 at 06:49
-
@Birju check my answer. – Chandresh Kachariya Jan 31 '17 at 07:07
-
@Andreas Oetjen my question is not duplicate. Please remove duplication tag from my question. – Birju Jan 31 '17 at 10:04
3 Answers
Use map rects. Here's an example using the map's current visible rect. With regards to your question, you could use convertRegion:toRectToView: to first convert your region to a MKMapRect beforehand.
MKMapPoint userPoint = MKMapPointForCoordinate(mapView.userLocation.location.coordinate);
MKMapRect mapRect = mapView.visibleMapRect; // find visible map rect
//MKMapRect mapRect = [self getMapRectUsingAnnotations:arrCordinate];//find custom map rect
BOOL inside = MKMapRectContainsPoint(mapRect, userPoint);
MKMapRect mapRect = mapView.visibleMapRect; Create your custom mapRect using your boundary region from multiple latitude and longitude
- (MKMapRect) getMapRectUsingAnnotations:(NSArray*)arrCordinate {
MKMapPoint points[[arrCordinate count]];
for (int i = 0; i < [arrCordinate count]; i++) {
points[i] = MKMapPointForCoordinate([arrCordinate[i] MKCoordinateValue]);
}
MKPolygon *poly = [MKPolygon polygonWithPoints:points count:[arrCordinate count]];
return [poly boundingMapRect];
}

- 667
- 2
- 13
- 31
-
Thank you for your reply regarding my question. In your answer i think i have to check everytime that is user's location inside maprect or not. Is there any other way to check without if condition more? For example CLLocationManager has it's own method didEnterRegion and didExitRegion. – Birju Jan 31 '17 at 07:11
-
i not have any own method but using didUpdateToLocation method through it's possible. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation; – Chandresh Kachariya Jan 31 '17 at 07:15
-
If i have to check everytime in didUpdateToLocation then is it work if my application is in kill mode? – Birju Jan 31 '17 at 07:32
-
@Birju kill mode mens terminate mode? Possible to connect with personal chat because interesting your question. – Chandresh Kachariya Jan 31 '17 at 08:30
-
-
After terminate app you can perform some activity than use background fetch mode from Capabilities - https://www.raywenderlich.com/143128/background-modes-tutorial-getting-started because background fetch mode is call every 3 to 4 min. – Chandresh Kachariya Jan 31 '17 at 09:11
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134457/discussion-between-chandresh-kachariya-and-birju). – Chandresh Kachariya Jan 31 '17 at 09:14
Try this code. This is based on Winding Number Algorithm. This works for complex shapes such as your red line.
typedef struct {
double lon;
double lat;
} LATLON;
// returns true if w/in region
bool chkInRegion(LATLON poi, int npoi, LATLON *latlon)
{
int wn = 0;
for (int i = 0 ; i < npoi-1 ; i++) {
if (latlon[i].lat <= poi.lat && latlon[i+1].lat > poi.lat) {
double vt = (poi.lat - latlon[i].lat)/(latlon[i+1].lat - latlon[i].lat);
if (poi.lon < (latlon[i].lon + (vt * (latlon[i+1].lon - latlon[i].lon)))) {
wn++;
}
} else if (latlon[i].lat > poi.lat && latlon[i+1].lat <= poi.lat) {
double vt = (poi.lat - latlon[i].lat)/(latlon[i+1].lat - latlon[i].lat);
if (poi.lon < (latlon[i].lon + (vt * (latlon[i+1].lon - latlon[i].lon)))) {
wn--;
}
}
}
return wn < 0;
}
// test data
LATLON llval[] = {
{100,100},
{200,500},
{600,500},
{700,100},
{400,300},
{100,100}
};
#define NLATLON (sizeof(llval)/sizeof(LATLON))
int main(int argc, char **argv) {
while (1) {
char buf[1024];
fprintf(stderr, "lon = ");
fgets(buf, sizeof(buf), stdin);
double lon = atof(buf);
fprintf(stderr, "lat = ");
fgets(buf, sizeof(buf), stdin);
double lat = atof(buf);
LATLON ltest;
ltest.lat = lat;
ltest.lon = lon;
if (chkInRegion(ltest, NLATLON, llval)) {
fprintf(stderr, "\n*** in region ***\n\n");
} else {
fprintf(stderr, "\n=== outside ===\n\n");
}
}
return 0;
}

- 794
- 2
- 7
- 17
Geofencing will not work on complex polygon shaped regions. May be you can solve the problem with some other approach. For instance divide the region into smaller CLCircularRegion and then develop aggregate logic for the case where you have to show notification for all those locationManager:didEnterRegion: and locationManager:didExitRegion: callbacks. But keep in mind that only a max of 20 simultaneous monitored regions per app are allowed.
Refer https://forums.developer.apple.com/thread/21323 phillippk1 suggestion for other possible approach.

- 39
- 7
-
Dios , thank you for your answer. I will check this and update you for the same. – Birju Jan 31 '17 at 07:30