Масштабировать, чтобы соответствовать области для всех аннотаций - в конечном итоге масштабирование между аннотациями

У меня проблема с размещением всех моих аннотаций на экране... иногда отображаются все аннотации, но иногда приложение увеличивает масштаб между двумя аннотациями, так что ни одна из них не видна... Я хочу, чтобы приложение всегда подогнать регион под аннотации и не увеличивать между ними... что я делаю не так?

if ([mapView.annotations count] == 2) { 
 CLLocationCoordinate2D SouthWest = location;
 CLLocationCoordinate2D NorthEast = savedPosition;

 NorthEast.latitude = MAX(NorthEast.latitude, savedPosition.latitude);
 NorthEast.longitude = MAX(NorthEast.longitude, savedPosition.longitude); 

 SouthWest.latitude = MIN(SouthWest.latitude, location.latitude);
 SouthWest.longitude = MIN(SouthWest.longitude, location.longitude);

 CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:SouthWest.latitude longitude:SouthWest.longitude];
 CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:NorthEast.latitude longitude:NorthEast.longitude];

 CLLocationDistance meter = [locSouthWest distanceFromLocation:locNorthEast];

 MKCoordinateRegion region;
 region.span.latitudeDelta = meter / 111319.5;
 region.span.longitudeDelta = 0.0;
 region.center.latitude = (SouthWest.latitude + NorthEast.latitude) / 2.0;
 region.center.longitude = (SouthWest.longitude + NorthEast.longitude) / 2.0;

 region = [mapView regionThatFits:region];
 [mapView setRegion:region animated:YES];

 [locSouthWest release];
 [locNorthEast release]; 
}

Любые идеи?

Спасибо!!


person Krismutt    schedule 24.12.2010    source источник


Ответы (2)


Используйте следующий код

-(void)zoomToFitMapAnnotations:(MKMapView*)mapView{
    if([mapView.annotations count] == 0)
        return;

    CLLocationCoordinate2D topLeftCoord;
    topLeftCoord.latitude = -90;
    topLeftCoord.longitude = 180;

    CLLocationCoordinate2D bottomRightCoord;
    bottomRightCoord.latitude = 90;
    bottomRightCoord.longitude = -180;

    for(FSMapAnnotation* annotation in 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;
    region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
    region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
    region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides

    region = [mapView regionThatFits:region];
    [mapView setRegion:region animated:YES];
}
person Satya    schedule 24.12.2010
comment
Пожалуйста, взгляните на обновление сверху! Что я делаю не так с вашим кодом? - person Krismutt; 24.12.2010
comment
вы можете использовать этот код вместо этого. Это работает для меня. Этот код предназначен для любого количества аннотаций (общий код). Или еще один раз попробуйте, заменив значение 1.2 на 1.1. - person Satya; 24.12.2010
comment
@iCoder: FSMapAnnotation — это класс пользовательских аннотаций, расширенный из класса MKAnnotation. - person Satya; 01.04.2011

Вместо:

region.span.latitudeDelta = meter / 111319.5;
 region.span.longitudeDelta = 0.0;
 region.center.latitude = (SouthWest.latitude + NorthEast.latitude) / 2.0;
 region.center.longitude = (SouthWest.longitude + NorthEast.longitude) / 2.0;

Попробуйте добавить:

region.span.latitudeDelta = fabs(NorthEast.latitude - SouthWest.latitude) * 1.2; 
region.span.longitudeDelta = fabs(SouthWest.longitude - NorthEast.longitude) * 1.2; 
region.center.latitude = NorthEast.latitude - (NorthEast.latitude - SouthWest.latitude) * 0.5;
region.center.longitude = NorthEast.longitude + (SouthWest.longitude - NorthEast.longitude) * 0.5;

И удаление:

CLLocationDistance meter = [locSouthWest distanceFromLocation:locNorthEast];
person Krismutt    schedule 24.12.2010