简体   繁体   中英

Zooming the mapview

I am doing an app based on mapview. I am getting the location(place) and converting it to co-ordinates. Using this, I am zooming into the exact location of the place specified.

But the problem I am facing is that, If I am specifying just the street name or the city name or the state name or the country name the zoom level is always the same. (The zoomlevel is always the same as that of the street.) If I am just specifying just the country name, the zoom level should be of the country level and not zooming right into any random street of the country.

Here is the code I have used in zooming.

-(void)zoomToFitMapAnnotations:(MKMapView*)mapViews
{
if([mapViews.annotations count] == 0)
    return;

CLLocationCoordinate2D topLeftCoordinate;
topLeftCoordinate.latitude = -90;
topLeftCoordinate.longitude = 180;

CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;

for(SJAddressAnnotation* annotation in mapViews.annotations)
{
    topLeftCoordinate.longitude = fmin(topLeftCoordinate.longitude, annotation.coordinate.longitude);
    topLeftCoordinate.latitude = fmax(topLeftCoordinate.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 = topLeftCoordinate.latitude - (topLeftCoordinate.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoordinate.longitude + (bottomRightCoord.longitude - topLeftCoordinate.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoordinate.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoordinate.longitude) * 1.1; // Add a little extra space on the sides

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

I have tried changing the span level unsuccessfully. Needs help in this case.

As I said earlier there is problem with one annotation. If you are retrieving the places from Google search then with each place they give approximate level of zoom(depending upon whether it is country/city). you can try by typing http://maps.google.com/maps/geo?q=asia&output=json in your browser. you can try different entries by replacing asia with china or newyork etc. Look at the Accuracy attribute of response in browser. It will be 0 for continents, 1 for countries etc.

If you have created annotations by yourself then you can attach a parameter to it which will relate to zoomlevel.

@interface AddressAnnotation : NSObject<MKAnnotation> {
double zoomLevel;
}
@property(readwrite,nonatomic) double zoomLevel ;
@end




 @implementation AddressAnnotation
    @synthesize zoomLevel;
    -(void)setZoomLevel (double) parameter
    {
    self.zoomLevel = parameter;
    }
@end

and finally assuming annot is your annotation then

  [annot setZoomLevel: .1]; //instead of .1 you can set different values

when you are displaying this annotation set region center as annotation coordinate and set span as annotation.zoomLevel.

MKCoordinateRegion region;
region.center.latitude = annotation.coordinate.latitude;
region.center.longitude = annotation.coordinate.longitude;
region.span.latitudeDelta = annotation.zoomLevel;
region.span.longitudeDelta = annotation.zoomLevel;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM