3

我在地图视图中只有一个注释。

我正在使用以下代码缩放到该注释图钉

- (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 的值,但无法获得解决方案。

任何建议,将不胜感激。

4

3 回答 3

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];
于 2013-10-22T14:28:17.390 回答
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;
于 2013-10-22T14:32:51.803 回答
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];

目前,它围绕纬度放大到一英里。如果我想或多或少地缩放,我将更改 latlong delta 值。

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

于 2014-03-31T06:27:20.713 回答