Nazik
Nazik

Reputation: 8434

How to reduce the zoom level of MKMapview with annotation?

I'm having only one annotation in the mapview.

I'm using the following code to zoom to that annotation pin

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

But it zooms to the extreme, I need to reduce the zoom level so the user can see some area around the annotation.

I tried changing the values of region.span, but can't get the solution.

Any suggestions would be appreciated.

Upvotes: 3

Views: 1446

Answers (3)

Nazik
Nazik

Reputation: 8434

I used the below code for zooming to the annotation,

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

Currently, it zooms to one mile around the lat long. If I want to zoom more or less, I'll change the latlong delta value.

ie, for 2 miles = 0.0144927536*2 and for 0.5 mile = 0.0144927536/2. I hope, it will be useful for others.

Upvotes: 0

Shehbaz Khan
Shehbaz Khan

Reputation: 1990

use this code to adjust your mapView zoom level

 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;

Upvotes: 1

wootage
wootage

Reputation: 936

Are you sure that this code :

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

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

doesn't return latitude/longitude = 0 ? Try to hardcode the span to 0.05 or something close.

The code below will center your map to your current position , you can change locationManager.coordinates with your annotation.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];

Upvotes: 1

Related Questions