UIMapView有时会显示错误的区域

时间:2013-07-28 20:46:49

标签: iphone ios objective-c mapkit core-location

当我用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;
}

安装应用程序后,我得到了这个:

screen1

之后我得到了这个观点:

screen2

知道什么是错的吗?

0 个答案:

没有答案