0

当我用 UIMapView 显示 UIViewController 时,在 viewDidLoad 中我将 PointAnnotation 添加到地图和数组中。然后我计算这些注释的区域并将该区域设置为映射。关键是有时(基本上在第一次安装应用程序后只有一次)即使我的数据是正确的,地图也会显示赤道区域。

-(void)viewDidLoad
{
NSLog(@"Spot: %@", _spot);

_annotations = [[NSMutableArray alloc] init];

[super viewDidLoad];


// Adding annotation of spot
CLLocationCoordinate2D annotationCoord;

annotationCoord.latitude = self.spot.latitude;
annotationCoord.longitude = self.spot.longitude;

POPointAnnotation *annotationPoint = [[POPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = self.spot.name;
annotationPoint.subtitle = self.spot.address;
annotationPoint.type = PointAnnotaitonTypeSpot;

[_annotations addObject:annotationPoint];
[((SpotsMapView *)self.view).mapView addAnnotation:annotationPoint];
[annotationPoint release];

// Adding annotation of user position
CLLocationCoordinate2D userCoord;

userCoord = [[POLocationManager sharedInstance] location].coordinate;

POPointAnnotation *userPoint = [[POPointAnnotation alloc] init];
userPoint.coordinate = userCoord;
userPoint.title = NSLocalizedString(@"Punkt startowy", @"Punkt startowy");
userPoint.type = PointAnnotationTypeStart;

[_annotations addObject:userPoint];
[((SpotsMapView *)self.view).mapView addAnnotation:userPoint];
[userPoint release];

[((SpotsMapView *)self.view).mapView setRegion:[self regionFromLocations] animated:YES];
[((SpotsMapView *)self.view).mapView setShowsUserLocation:YES];

}

和这里的地区:

- (MKCoordinateRegion)regionFromLocations {
CLLocationCoordinate2D upper = [[self.annotations objectAtIndex:1] coordinate];
CLLocationCoordinate2D lower = [[self.annotations objectAtIndex:1] coordinate];

NSLog(@"Loc Upper: %f, %f", upper.latitude, upper.longitude);
NSLog(@"Loc Lower: %f, %f", lower.latitude, lower.longitude);

// FIND LIMITS
for(MKPointAnnotation *eachLocation in self.annotations) {
    if([eachLocation coordinate].latitude > upper.latitude) upper.latitude = [eachLocation coordinate].latitude;
    if([eachLocation coordinate].latitude < lower.latitude) lower.latitude = [eachLocation coordinate].latitude;
    if([eachLocation coordinate].longitude > upper.longitude) upper.longitude = [eachLocation coordinate].longitude;
    if([eachLocation coordinate].longitude < lower.longitude) lower.longitude = [eachLocation coordinate].longitude;
}

NSLog(@"Loc Upper: %f, %f", upper.latitude, upper.longitude);
NSLog(@"Loc Lower: %f, %f", lower.latitude, lower.longitude);

// FIND REGION
MKCoordinateSpan locationSpan;
locationSpan.latitudeDelta = upper.latitude - lower.latitude + 0.003;
locationSpan.longitudeDelta = upper.longitude - lower.longitude+ 0.003;
CLLocationCoordinate2D locationCenter;
locationCenter.latitude = (upper.latitude + lower.latitude) / 2;
locationCenter.longitude = (upper.longitude + lower.longitude) / 2;

MKCoordinateRegion region = MKCoordinateRegionMake(locationCenter, locationSpan);
return region;
}

安装应用程序后,我得到以下信息:

屏幕1

之后我得到了这个观点:

屏幕2

知道有什么问题吗?

4

0 回答 0