2013-10-22 67 views
3

我在mapview中只有一個註釋。如何使用註解縮小MKMapview的縮放級別?

我用下面的代碼來放大某個註釋銷

- (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(id<MKAnnotation> 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) * 0.1; 

    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 0.1; 

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

但它放大到極致,我需要減少縮放級別,以便用戶可以看到周圍的註釋部分地區。

我試着改變region.span的值,但無法獲得解決方案。

任何建議,將不勝感激。

回答

0

我用下面的代碼進行變焦的註釋,

CLLocation *location = [[CLLocation alloc] initWithLatitude:[@"My annotation's latitude" doubleValue] longitude:[@"My annotation's longitude" doubleValue]]; 
MKCoordinateRegion mkr; 
mkr.center = location.coordinate; 
MKCoordinateSpan span; 
span.latitudeDelta = 0.0144927536; 
span.longitudeDelta = 0.0144927536; 
mkr.span = span; 
[mapView setRegion:mkr animated:YES]; 

目前,縮放到周圍的1英里lat長。如果我想縮放或多或少,我會改變latlong delta值。

即,對於2英里= 0.0144927536 * 2和對於0.5英里= 0.0144927536/2。我希望,這對其他人有用。

+0

好的工作。真的很有幫助。你能解釋一下,我怎樣才能以不同的縮放級別在MKMapView中添加註解或疊加層。我嘗試了很多解決方案,但都沒有運氣 – ataurrehman

+0

在我的場景中,地圖上有數百個標記。我想在不同的縮放級別上顯示它們。 – ataurrehman

1

你確定這個代碼:

region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 0.1; 

region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 0.1; 

不返回緯度/經度= 0? 嘗試將跨度硬編碼爲0.05或其他值。

下面的代碼將您的地圖居中放置到當前位置,您可以使用您的annotation.coordinates更改locationManager.coordinates。

CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[locationManager startUpdatingLocation]; 

MKCoordinateSpan span; 
MKCoordinateRegion region; 
span.latitudeDelta = 0.05; 
span.longitudeDelta = 0.05;  
location.latitude = locationManager.location.coordinate.latitude; 
location.longitude = locationManager.location.coordinate.longitude; 
NSLog(@"%f",location.latitude); 
region.span = span; 
region.center = location; 

[_mapVIew setRegion:region animated:YES]; 
1

使用此代碼來調整自己的MapView的縮放級別

MKCoordinateRegion mapRegion; 
    // set the center of the map region to the now updated map view center 
    mapRegion.center = self.mapView.centerCoordinate; 
    mapRegion.span.latitudeDelta = 0.1; 
    mapRegion.span.longitudeDelta = 0.1; 
    mapView.region=mapRegion;