How to properly set the Radius of CLRegion?

2019-07-30 03:38发布

This is my code for creating a geofence:

Geofence *geofence = [[Geofence alloc] init];
[geofence setRadius:100.0];
[geofence setIdentifier: @"Fence1"];
[geofence setLocation:currentlocation];


CLRegion *region1 =  [[CLCircularRegion alloc] initWithCenter:[geofence.location coordinate]
                                               radius:geofence.radius
                                               identifier:geofence.identifier];

region1.notifyOnEntry=YES;
region1.notifyOnExit=YES;
[self.locationManager startMonitoringForRegion:region1];

I could say that, the above code works, meaning the geofence have been created. But there's a problem I've been encountering.

I have a .gpx files (in which I will be using for simulation of my current location), their distances from the center of the region/geofence are as follows: 55m, 90m, 150m, 200m, 300m, 500m, 1Km, 5Km and 10Km.

My problem is, its seems that my geofence radius is not really 100m, because when I simulate using gpx files whose distance from the center of the geofence are 55m, 90m, 150, 200m, 300m, 500m, 1km. The delegate methods of LocationManager -didEnterRegion and -didExitRegion is making a respond that I'm within my Geofence whose radius is just "100m".

Only the gpx files with distances from my geofence center that are 5Km and 10km responds from the said delegate methods that I'm outside the Geofence.

Can you help me resolve this scenario guys?

enter image description here

1条回答
Root(大扎)
2楼-- · 2019-07-30 03:56

I think the problem may be that you are using the same variables to initialize your region with as the ones you are using to initialize your geofence as far as their center coordinates are concerned. Thus one always thinks its within the other.

查看更多
登录 后发表回答