0
我在Objective-C上只有幾個月的經驗。如何根據geocodeAddressString的搜索結果設置焦點中心
我有一個代碼來搜索客戶地址列表,然後顯示這些地址的註釋。由於客戶覆蓋很多地區,我希望使地圖視圖的中心和跨度適應結果。
因此,我計劃記錄所有搜索到的地點標記並計算平均的經度和緯度。下面是我的代碼
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];
但問題是,像This problem
計時問題的setRegion是要獲得這些地點標記之前執行。在該鏈接的解決方案中,我應該在完成塊中調用一個方法。但是這個解決方案不適用於多地址問題,因爲我需要在setRegion之前將所有地點標記添加到我的方法中。
是否有任何方法在從geocodeAddressString獲取所有結果後執行?
在此先感謝。