How to set focus center based on the searched result of geocodeAddressString

StackOverflow https://stackoverflow.com/questions/12136022

  •  28-06-2021
  •  | 
  •  

Domanda

I have only a couple of months experience on Objective-C.

I have a code to search a list of customer address, then show the annotation of these addresses. As the customers cover many regions, I would like to make map view's center and span adapt to the results.

Therefore, I planed to record all searched place marks and calculate an average latitude and longitude. Below is my code

self.mapView.mapType = MKMapTypeStandard; 
for (Customer *customer in customers) {
    CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
    NSString *customerAddress = [Customer getWholeAddressOfCustomer:customer];
    NSString *color = @"";
    if([customer.status isEqualToString:@"1"])
    {
        color = @"Green";
    }else if ([customer.status isEqualToString:@"2"]) {
        color = @"Yellow";
    }else if ([customer.status isEqualToString:@"3"])
    {
        color = @"Red";
    }else {
        color = customer.status;
    }
    [geoCoder geocodeAddressString:customerAddress completionHandler:^(NSArray *placemarks, NSError *error) 
     {
         [NSThread sleepForTimeInterval:0.25];
         if (placemarks.count == 0)
         {

             NSLog(@"No place for customer %@ was found",customerAddress);
         }

         CLPlacemark *placemark = [placemarks objectAtIndex:0];

         if(placemark.location.coordinate.latitude != 0.000000 && placemark.location.coordinate.longitude != 0.000000)
         {
             CustomerAnnotation *annotation = [[CustomerAnnotation alloc]initWithCoordinate:placemark.location.coordinate andName:customer.name andNumber:customer.customerNo andColor:color];  

             [self.mapAnnotations addObject:annotation];

         }

     }];

}
CLLocationDegrees totalLatitude=0;
CLLocationDegrees totalLongitude = 0;
for (int i=0; i < [self.mapAnnotations count]; i++) {
    totalLatitude += [(CustomerAnnotation *)[self.mapAnnotations objectAtIndex:i] coordinate].latitude;
    totalLongitude += [(CustomerAnnotation *)[self.mapAnnotations objectAtIndex:i] coordinate].longitude;
    [self.mapView addAnnotation:[self.mapAnnotations objectAtIndex:i]];

}

MKCoordinateRegion focusRegion;
focusRegion.center.latitude = totalLatitude/[self.mapAnnotations count];
focusRegion.center.longitude = totalLongitude/[self.mapAnnotations count];
focusRegion.span.latitudeDelta = 20; //will modify this parameter later to self adapt the map
focusRegion.span.longitudeDelta = 20;
[self.mapView setRegion:focusRegion animated:YES];

But the problem is the timing issue like This problem

The setRegion is executed prior to obtaining those place marks. In the solution raised of that link, I should call a method in the completion block. But this solution is not suitable to a multiple address issue as I need to add all place marks to my method before I setRegion.

Is there anyway to make a method be executed after obtaining all results from geocodeAddressString?

Thanks in advance.

È stato utile?

Soluzione

Issue resolved.

Outside of the block, I used a counter to record the annotation added.

__block int customerCount = 0; 

When this count equals to the whole number, auto focus the central area of all customer.

if (customerCount == [customers count]) {
   //Set the focus
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top