我有一个视图控制器,它以模态方式呈现,显示地图视图。所以地图视图使用以下代码可以正常工作,但是有 2 个未使用的变量(长双精度 x1,x2),当我删除它们时,CLLocation 总是在视图控制器第三次出现时返回坐标的 nan 值。temp1 值将是 nan 第三次,从那时起。
为什么我需要这两个未使用的变量??????,这是我的问题。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
navigationItemTitle.prompt = name;
if ([mapDict count] > 0)
{
id val = nil;
NSArray *values = [mapDict allValues];
long double x1, x2, temp1, temp2; //x1, x2 unused but necessary
for (int i = 0; i < [mapDict count]; i++)
{
val = [values objectAtIndex:i];
temp1 += ((CLLocation *)val).coordinate.latitude;
temp2 += ((CLLocation *)val).coordinate.longitude;
}
temp1 /= [values count];
temp2 /= [values count];
//NSLog(@"%Lf", temp1);
//NSLog(@"%Lf", temp2);
CLLocationCoordinate2D centerCooordinate = CLLocationCoordinate2DMake(temp1, temp2);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(centerCooordinate, 10000000, 10000000);
[mapView setRegion:[mapView regionThatFits:region]];
for(id key in mapDict)
{
id value = [mapDict objectForKey:key];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = ((CLLocation *)value).coordinate;
point.title = key;
point.subtitle = [NSString stringWithFormat:@"%f\t%f", point.coordinate.latitude, point.coordinate.longitude];
[mapView addAnnotation:point];
}
}
}