Отображение MKMapView для одновременного отображения нескольких аннотаций и текущего местоположения пользователя - PullRequest
1 голос
/ 21 декабря 2011

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

Ответы [ 2 ]

1 голос
/ 21 декабря 2011

привет, вы должны установить вид карты в соответствии с вашей координатой аннотации в области карты.

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

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

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

for(DDAnnotation* 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 = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
}
0 голосов
/ 22 декабря 2011

Вам нужно установить регион вашего просмотра карты в соответствии с двумя выводами -

float maxLat = current_location_latitude;
float maxLon = current_location_longitude;
float minLat = other_location_latitude;
float minLon = other_location_longitude;

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake((maxLat + minLat) / 2.0, (maxLon + minLon) / 2.0), 1000.0, 1000.0);
region.span.latitudeDelta = MAX(region.span.latitudeDelta, (maxLat - minLat) * 1.20);
region.span.longitudeDelta = MAX(region.span.longitudeDelta, (maxLon - minLon) * 1.20);
[self.mapView setRegion:region animated:YES];
...