0
votes

Since the location Manager can monitor a maximum of 10 locations ..

I need to know which Region the user is in, in order to monitor the associated locations ..

I tried this but the loop does not break ..

The loop should break because the Region initiated from index 2 at the array contains the specified coordinates "currentCoordinate" ..

I wonder if I need to startMonitorForRegion using a LocationManager instance prior to using this method "containsCoordinate" ..

Can you tell me what am I doing wrong ??

-(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; 
}

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

Please if you can help me, Please do ..

Update

I test against currentLocation .. Because in - (void)locationManager:(CLLocationManager *)manage didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

I have stored the recent coordinates in currentCoordinates ..

May be I need to test on the Device ..

This is the Result Form 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

.

.

.

The same trend continues

.

. 

. 


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
Hi, Can you edit your question and add the output from the NSLogs please? It looks as though it should work . . .deanWombourne
This is the result From the NSLog ..Sara2022
Please just edit your question to provide additional information. Answers should not be 'replies', they are reserved for solutions.Tim Post

2 Answers

0
votes

You say the loop should break on index 2, but the coordinates with which you're doing the hit test aren't even determined in the loop.

Maybe you meant to be testing against rCoordinate and not currentCoordinates.

[testRegion containsCoordinate:rCoordinate];
0
votes

I have solved the problem for whom it concerns I should use CLLocationDegrees not CLLocationDistance and in the following way

-(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; 

}