2014-02-06 5 views
1

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

-(void)zoomToFitNearestAnnotationsAroundUserLocation { 


    MKMapPoint userLocationPoint = MKMapPointForCoordinate(self.restaurantsMap.userLocation.coordinate); 
    MKCoordinateRegion region; 
    if ([self.restaurantsMap.annotations count] > 1) { 

     for (id<MKAnnotation> annotation in self.restaurantsMap.annotations) { 

      MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); 
      CLLocationDistance distanceBetweenAnnotationsAndUserLocation = MKMetersBetweenMapPoints(annotationPoint, userLocationPoint); 


      region = MKCoordinateRegionMakeWithDistance(self.restaurantsMap.userLocation.coordinate, distanceBetweenAnnotationsAndUserLocation, distanceBetweenAnnotationsAndUserLocation); 


     } 

     [self.restaurantsMap setRegion:region animated:YES]; 

    } 



} 

Как бы мне удалось сохранить 2-3 из ближайших расстояний и сделать область на основе этой информации?

ответ

3

Если вы разрабатываете iOS 7 и выше, вы можете просто сохранить массив аннотаций, отсортированный по расстоянию между местоположением пользователя и аннотацией, а затем взять первые 3 аннотации. После этого вы можете использовать showAnnotations:animated:, который будет позиционировать карту так, чтобы все аннотации были видны.

Вот еще один способ (из here):

MKMapRect zoomRect = MKMapRectNull; 
for (id <MKAnnotation> annotation in mapView.annotations) 
{ 
    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); 
    MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1); 
    zoomRect = MKMapRectUnion(zoomRect, pointRect); 
} 
[mapView setVisibleMapRect:zoomRect animated:YES]; 

//You could also update this to include the userLocation pin by replacing the first line with 
MKMapPoint annotationPoint = MKMapPointForCoordinate(mapView.userLocation.coordinate); 
MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1); 

Конечно, вы должны обновить второе решение использовать только самые близкие точки аннотаций, но вы уже знаете, как найти те, так что не должен» t проблема.

2

магазина аннотации в массиве

Добавьте эту функцию и изменить расстояние, как вам нужно

- (MKCoordinateRegion)regionForAnnotations:(NSArray *)annotations 
{ 
MKCoordinateRegion region; 

if ([annotations count] == 0) 
{ 
    region = MKCoordinateRegionMakeWithDistance(_mapView.userLocation.coordinate, 1000, 1000); 
} 

else if ([annotations count] == 1) 
{ 
    id <MKAnnotation> annotation = [annotations lastObject]; 
    region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 1000, 1000); 
} else { 
    CLLocationCoordinate2D topLeftCoord; 
    topLeftCoord.latitude = -90; 
    topLeftCoord.longitude = 180; 

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

    for (id <MKAnnotation> annotation in annotations) 
    { 
     topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude); 
     topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); 
     bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); 
     bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); 
    } 

    const double extraSpace = 1.12; 
    region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude)/2.0; 
    region.center.longitude = topLeftCoord.longitude - (topLeftCoord.longitude - bottomRightCoord.longitude)/2.0; 
    region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * extraSpace; 
    region.span.longitudeDelta = fabs(topLeftCoord.longitude - bottomRightCoord.longitude) * extraSpace; 
} 

return [self.mapView regionThatFits:region]; 
} 

и назвать его, как этот

MKCoordinateRegion region = [self regionForAnnotations:_locations]; 
[self.mapView setRegion:region animated:YES]; 

Теперь масштаб должен соответствовать всем аннотациям в массиве

Удачи !!

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