2010-09-08 17 views
0

alt textiPhone Région Carte Weirdness

L'image ci-joint est ce qui arrive parfois lorsque je tente de la taille d'un MKMapView pour s'adapter à un ou plusieurs repères. C'est intermittent, mais l'affichage est toujours exactement dans la même position.

Voici le code:

// loc1 is always non-null, and is equal to one of the annotation locations 

CLLocationCoordinate2D topLeftCoord = loc1.coordinate; 
CLLocationCoordinate2D bottomRightCoord = loc1.coordinate; 
for(UserPlacemark* annotation in self.mapView.annotations) 
{ 
    topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); 
    topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);  
    bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); 
    bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); 
} 

MKCoordinateRegion region; 
double k = 0.01; 
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.25; 
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5; 
region.span.latitudeDelta = k + fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.25; // Add a little extra space on the sides 
region.span.longitudeDelta = k + fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.5; // Add a little extra space on the sides 

// only zoom if region doesn't fit, or too small 
CGRect newRect = [mapView convertRegion:region toRectToView:mapView]; 
double MIN_SIZE_PIXELS = 50.0; 
double rectSizePixels = newRect.size.width+newRect.size.height; 
if (!CGRectContainsRect(mapView.bounds, newRect) || rectSizePixels < MIN_SIZE_PIXELS) 
{ 
     region = [mapView regionThatFits:region]; 
     [mapView setRegion:region animated:TRUE]; 
} 

Répondre

0

Le problème dans le code ci-dessus est qu'une instance MKUserAnnotation est placé dans la liste par le cadre placemark de la carte. Cela ne correspond pas toujours à la position réelle de l'utilisateur, au moins dans le simulateur.

Il est préférable d'éviter d'itérer sur la liste d'annotations et de calculer les limites min/max de votre repère à l'aide de vos objets directement.