如何使用注释降低MKMapview的缩放级别?

时间:2013-10-22 14:02:19

标签: iphone ios objective-c mkmapview zoom

我在mapview中只有一个注释。

我正在使用以下代码缩放到该注释引脚

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

任何建议都将不胜感激。

3 个答案:

答案 0 :(得分:1)

您确定此代码:

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

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

不返回纬度/经度= 0? 尝试将跨度硬编码为0.05或近似值。

下面的代码会将您的地图置于当前位置的中心,您可以使用您的注释更改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 :(得分: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;

答案 2 :(得分: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。我希望,这对其他人有用。