2010-09-08 4 views
0

alt textiPhone Карта региона Weirdness

Прилагаемое изображение, что иногда происходит, когда я пытаюсь установленный размер MKMapView, чтобы соответствовать один или несколько меткам. Это прерывисто, но дисплей всегда находится в точно таком же положении.

Вот код:

// 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]; 
} 

ответ

0

Проблема в приведенной выше коде является то, что экземпляр MKUserAnnotation находится в списке метки в рамках карты. Это не всегда соответствует фактическому положению пользователя, по крайней мере, в симуляторе.

Лучше избегать итерации по списку аннотаций и вместо этого вычислить границы min/max вашей метки с помощью ваших объектов напрямую.

Смежные вопросы