2014-02-06 117 views
1

我想縮放我的地圖視圖以顯示至少一個最近註釋的註釋,儘可能最大的縮放以及用戶位置。我試過以下內容:MKMapView - 縮放以適應用戶位置周圍的最近註釋

-(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); 

當然你要更新第二溶液中,僅使用最接近的標註點,但你已經知道如何找到那些讓不該」不成問題。

2

Store中的數組中的

註釋添加此功能,改變的距離,因爲你需要

- (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]; 

現在變焦應該適合所有註釋在陣列中

祝您好運!

相關問題