我正在使用CLRegion方法“containsCoordinate”,但我没有得到写入结果?

时间:2011-02-19 13:25:09

标签: iphone objective-c

由于位置管理员最多可以监控10个位置..

我需要知道用户所在的区域,以便监控相关位置..

我尝试了这个,但循环没有破坏..

循环应该中断,因为从数组索引2开始的Region包含指定的坐标“currentCoordinate”..

在使用此方法“containsCoordinate”之前,我想知道是否需要使用LocationManager实例启动MonitorForRegion ..

你能告诉我我做错了什么吗?

-(void) chooseRegion {

int Count = mosqueRegionsArray.count;
NSLog(@"In Choose Region: Regions Count %d",Count);

currentCoordinates.longitude = lastKnownLocation.coordinate.longitude;
currentCoordinates.latitude = lastKnownLocation.coordinate.latitude; 
int index = 0; 
CLRegion *testRegion;
for(index=0; index<Count; index++)
{
    CLLocationCoordinate2D rCoordinate;
    rCoordinate.latitude =[(Region *)[mosqueRegionsArray objectAtIndex:index] latitude];
    rCoordinate.longitude =[(Region *)[mosqueRegionsArray objectAtIndex:index] longitude];
    NSLog(@"Region Coordinate %f, %f", rCoordinate.latitude, rCoordinate.longitude);

    CLLocationDistance rRadius  = [(Region *)[mosqueRegionsArray objectAtIndex:index] radius];

    NSLog(@"%f", rRadius); 

    NSString *rCity = [(Region *)[mosqueRegionsArray objectAtIndex:index] name];

    testRegion = [[CLRegion alloc] initCircularRegionWithCenter:rCoordinate radius:rRadius identifier:rCity];

    NSLog(@"region Index %d", index);

    NSLog(@"New Coordinate %f, %f", currentCoordinates.latitude, currentCoordinates.longitude);
    if ([testRegion containsCoordinate:currentCoordinates]) 
        goto outer;
    else 
        NSLog(@"Not in Region");

    //[locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];


}
outer:
        [testRegion release];


return; 
}

=============================================== ===================

如果你能帮助我,请做..

更新

我对currentLocation进行测试.. 因为在 - (void)locationManager :( CLLocationManager *)管理didUpdateToLocation :( CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

我已将最近的坐标存储在currentCoordinates ..

可能我需要在设备上进行测试..

这是结果表格NSLog ..

2011-02-20 22:06:32.614 MosquesDBNav2[4939:207] Mosques No 349

2011-02-20 22:06:32.615 MosquesDBNav2[4939:207] Regions No 54

2011-02-20 22:06:32.615 MosquesDBNav2[4939:207] Starting Location Updates

2011-02-20 22:06:33.776 MosquesDBNav2[4939:207] last known location 25.333806, 51.483350

2011-02-20 22:06:33.783 MosquesDBNav2[4939:207] In Choose Region: Regions Count 54

2011-02-20 22:06:33.783 MosquesDBNav2[4939:207] Region Coordinate 25.342252, 51.470266

2011-02-20 22:06:33.784 MosquesDBNav2[4939:207] 1000.000000

2011-02-20 22:06:33.785 MosquesDBNav2[4939:207] region Index 0

2011-02-20 22:06:33.786 MosquesDBNav2[4939:207] New Coordinate 25.333806, 51.483350

2011-02-20 22:06:33.787 MosquesDBNav2[4939:207] Not in Region

2011-02-20 22:06:33.788 MosquesDBNav2[4939:207] Region Coordinate 25.350823, 51.466146

2011-02-20 22:06:33.789 MosquesDBNav2[4939:207] 1400.000000

2011-02-20 22:06:33.789 MosquesDBNav2[4939:207] region Index 1

2011-02-20 22:06:33.790 MosquesDBNav2[4939:207] New Coordinate 25.333806, 51.483350

2011-02-20 22:06:33.790 MosquesDBNav2[4939:207] Not in Region

2011-02-20 22:06:33.792 MosquesDBNav2[4939:207] Region Coordinate 25.326581, 51.492668

2011-02-20 22:06:33.792 MosquesDBNav2[4939:207] 1200.000000

2011-02-20 22:06:33.793 MosquesDBNav2[4939:207] region Index 2

2011-02-20 22:06:33.794 MosquesDBNav2[4939:207] New Coordinate 25.333806, 51.483350

2011-02-20 22:06:33.794 MosquesDBNav2[4939:207] Not in Region

.

.

.

同样的趋势仍在继续

.

. 

. 


2011-02-20 22:06:33.939 MosquesDBNav2[4939:207] Region Coordinate 25.248713, 51.518103

2011-02-20 22:06:33.939 MosquesDBNav2[4939:207] 860.000000

2011-02-20 22:06:33.940 MosquesDBNav2[4939:207] region Index 52

2011-02-20 22:06:33.940 MosquesDBNav2[4939:207] New Coordinate 25.333806, 51.483350

2011-02-20 22:06:33.941 MosquesDBNav2[4939:207] Not in Region

2011-02-20 22:06:33.941 MosquesDBNav2[4939:207] Region Coordinate 25.259193, 51.533467

2011-02-20 22:06:33.941 MosquesDBNav2[4939:207] 530.000000

2011-02-20 22:06:33.942 MosquesDBNav2[4939:207] region Index 53

2011-02-20 22:06:33.943 MosquesDBNav2[4939:207] New Coordinate 25.333806, 51.483350

2011-02-20 22:06:33.943 MosquesDBNav2[4939:207] Not in Region

2 个答案:

答案 0 :(得分:0)

你说循环应该在索引2处中断,但你在进行命中测试的坐标甚至都没有在循环中确定。

也许您打算对rCoordinate而不是currentCoordinates进行测试。

[testRegion containsCoordinate:rCoordinate];

答案 1 :(得分:0)

我已经解决了它所关心的问题 我应该使用CLLocationDegrees而不是CLLocationDistance,并采用以下方式

-(void) chooseRegion {

int Count = mosqueRegionsArray.count;
NSLog(@"In Choose Region: Regions Count %d",Count);


int index = 0; 

for(index=0; index<Count; index++)
{
    CLLocationCoordinate2D rCoordinate;
    rCoordinate.latitude =[(Region *)[mosqueRegionsArray objectAtIndex:index] latitude];
    rCoordinate.longitude =[(Region *)[mosqueRegionsArray objectAtIndex:index] longitude];
    NSLog(@"Region Coordinate %f, %f", rCoordinate.latitude, rCoordinate.longitude);

    //CLLocationDistance rRadius  = [(Region *)[mosqueRegionsArray objectAtIndex:index] radius];
    CLLocationDegrees rRadius = [(Region *)[mosqueRegionsArray objectAtIndex:index] radius];
    if (rRadius > self.locationManager.maximumRegionMonitoringDistance)
        rRadius = self.locationManager.maximumRegionMonitoringDistance;
    NSLog(@"%f", rRadius); 

    NSString *rCity = [(Region *)[mosqueRegionsArray objectAtIndex:index] name];

    CLRegion *testRegion = [[CLRegion alloc] initCircularRegionWithCenter:rCoordinate radius:rRadius identifier:rCity];

    NSLog(@"region Index %d", index);

    [locationManager startMonitoringForRegion:testRegion desiredAccuracy:kCLLocationAccuracyHundredMeters];

    NSLog(@"New Coordinate %f, %f", currentLocation.latitude, currentLocation.longitude);

    if ([testRegion containsCoordinate:currentLocation]) 
        break;
    else 
        NSLog(@"Not in Region");

    [locationManager stopMonitoringForRegion:testRegion];
    [testRegion release];


}// end of for loop 

[self startRegionMonitoring:index]; 
return; 

}