HIII,
我使用位置管理器实现了地理围栏。
我在这里创建区域
在这里输入代码
在视图中加载我创建对象
//用于地理围栏跟踪
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLLocationAccuracyBest;
[locationManager startMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
-(void)TraceGeofenceLocation{
for (int k=0; k< [self.chGeofenceType count]; k++) {
NSString *chTracLati = [NSString stringWithFormat:@"%f",[[self.chGeofenceLatitudes objectAtIndex:k] doubleValue]];
NSString *chTracLong = [NSString stringWithFormat:@"%f",[[self.chGeofeceLongitudes objectAtIndex:k] doubleValue]];
NSString *chTracRadius = [NSString stringWithFormat:@"%f",[[self.chGeofenceRadius objectAtIndex:k] doubleValue]];
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(chTracLati.doubleValue, chTracLong.doubleValue);
if ([[self.chGeofenceType objectAtIndex:k] intValue] == 1) {
region = [[CLRegion alloc]initCircularRegionWithCenter:coord radius:chTracRadius.doubleValue identifier:@"Restricted"];
}
else if ([[self.chGeofenceType objectAtIndex:k] intValue] == 2){
region = [[CLRegion alloc]initCircularRegionWithCenter:coord radius:chTracRadius.doubleValue identifier:@"SafeZone"];
}
else{
region = [[CLRegion alloc]initCircularRegionWithCenter:coord radius:chTracRadius.doubleValue identifier:@"Curfew"];
}
[region setNotifyOnEntry:YES];
[region setNotifyOnExit:YES];
[locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];
}
}
我在我的委托方法中测试了它是否会输入任何区域。
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
locationnew = locations.lastObject;
self.Latit=[NSString stringWithFormat:@"%f", locationnew.coordinate.latitude];
self.Longi=[NSString stringWithFormat:@"%f",locationnew.coordinate.longitude];
Speed = [[NSString stringWithFormat:@"%f",[locationnew speed]] floatValue];
NSLog(@"Speed :%f Latitude :%@ Longitude :%@",Speed,self.Latit,self.Longi);
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
if ([region.identifier isEqualToString:@"Restricted"]) {
[self sendNotificationtoServerwithtype:@"1"];
}
else if ([region.identifier isEqualToString:@"Curfew"]){
[self sendNotificationtoServerwithtype:@"3"];
}
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
if ([region.identifier isEqualToString:@"SafeZone"]){
[self sendNotificationtoServerwithtype:@"2"];
}
}
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
if (state == CLRegionStateInside){
NSLog(@"is in target region");
}else{
NSLog(@"is out of target region");
}
}
我的委托方法更新位置正在调用,我进入控制台, 但是进入区域,从区域退出并且确定DetermineState没有调用...
请有人帮帮我。
提前致谢
答案 0 :(得分:0)
startUpdatingLocation 和 startMonitoringSignificantLocationChanges 是互斥的。如果您想要 startMonitoringSignificantLocationChanges 来查找地理围栏,请务必先调用 stopUpdatingLocation 。
另请注意,地理围栏检测并不是非常精确。平均来说,你可以分辨几百米。我发现 didEnterRegion 通常需要几分钟才能调用,如果有的话。
注意:您当前正在调用[locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];这个问题是kCLLocationAccuracyBest是一个非常小的区域。 (kCLLocationAccuracyBest代码值为1.0)。这意味着当您进入直径1.0米的区域时,系统会要求系统通知您。由于地理围栏检测具有数百米的精度,因此可能永远不会被称为。相反,您应该将精度设置为更低的值:[locationManager startMonitoringForRegion:region desiredAccuracy: 200.0 ];
祝你好运。