0

I have already created Mapview with storyboard and JSON to placed huge of pins on mapview. Now my problem is whenever I entered into mapview Its showing some other area. Once I entered mapview I need to go zooming animation with show on pins dropped area.

Helpme
  • 59
  • 5
  • Check the comment of "Abhishek Bedi" on "Matthew Frederick's" accepted answer to this question: [Zooming MKMapView to fit annotation pins?](http://stackoverflow.com/questions/4680649/zooming-mkmapview-to-fit-annotation-pins) – Bista Jan 22 '16 at 05:02
  • may be this is all you want: [try this](http://brianreiter.org/2012/03/02/size-an-mkmapview-to-fit-its-annotations-in-ios-without-futzing-with-coordinate-systems/) – Ronak Chaniyara Jan 22 '16 at 05:03
  • @RonakChaniyara almost your answer is very close for my question. I will try and let you know. Thx! – Helpme Jan 22 '16 at 05:26

2 Answers2

0
    #define MINIMUM_ZOOM_ARC -2400 //approximately 1 miles (1 degree of arc ~= 69 miles)
    #define ANNOTATION_REGION_PAD_FACTOR 1.50
    #define MAX_DEGREES_ARC 360   
-(void)zoomMapViewToFitAnnotations:(MKMapView *)mapView animated:(BOOL)animated
    {

    NSArray *annotations = mapView.annotations;
    if (annotations.count == 0) {
        return; //bail if no annotations
    }


    MKMapPoint points[annotations.count];
    for( int i = 0; i < annotations.count; i++ ) {
        id annotation = [annotations objectAtIndex:i];
        CLLocationCoordinate2D coordinate = [(id <MKAnnotation>)annotation coordinate];
        points[i] = MKMapPointForCoordinate(coordinate);
    }
    //create MKMapRect from array of MKMapPoint
    MKMapRect mapRect = [[MKPolygon polygonWithPoints:points count:annotations.count] boundingMapRect];
    //convert MKCoordinateRegion from MKMapRect
    MKCoordinateRegion region = MKCoordinateRegionForMapRect(mapRect);

    //add padding so pins aren't scrunched on the edges
    region.span.latitudeDelta  *= ANNOTATION_REGION_PAD_FACTOR;
    region.span.longitudeDelta *= ANNOTATION_REGION_PAD_FACTOR;
    //but padding can't be bigger than the world
    if( region.span.latitudeDelta > MAX_DEGREES_ARC ) {
        region.span.latitudeDelta  = MAX_DEGREES_ARC;
    }
    if( region.span.longitudeDelta > MAX_DEGREES_ARC ) {
        region.span.longitudeDelta = MAX_DEGREES_ARC;
    }

    //and don't zoom in stupid-close on small samples
    if( region.span.latitudeDelta  < MINIMUM_ZOOM_ARC )
    {
        region.span.latitudeDelta  = MINIMUM_ZOOM_ARC;
    }
    if( region.span.longitudeDelta < MINIMUM_ZOOM_ARC )
    {
        region.span.longitudeDelta = MINIMUM_ZOOM_ARC;
    }
    //and if there is a sample of 1 we want the max zoom-in
    //instead of max zoom-out
    if(annotations.count == 1 ) {
        region.span.latitudeDelta = MINIMUM_ZOOM_ARC;
        region.span.longitudeDelta = MINIMUM_ZOOM_ARC;
    }
    [mapView setRegion:region animated:animated];
}

use this code to zoom map on particular pin on map view... this is work in my projects.

yankit Patel
  • 331
  • 1
  • 12
0

This will help you

    - (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(MapAnnotation *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) * 1.1; 

    // Add a little extra space on the sides 
    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; 

    // Add a little extra space on the sides 
    region = [mapView regionThatFits:region]; 
    [mapView setRegion:region animated:YES]; 
}
viratpuar
  • 524
  • 1
  • 5
  • 22