Search code examples
iosobjective-cmkmapviewzooming

mapkit zoom to maximum scale objective c


I am using mapkit .I have developed simple storyboard application .

1-The mapkit should zoom to maximum scale to show user location on loading mapkit.

2-On clicking loc.png the map should load the description of location with title and subtitle and detail about location

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView * annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"testAnnotationView"];
    if(annotationView == nil){
        annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"testAnnotationView"];
        annotationView.image = [UIImage imageNamed:@"loc.png"];
        annotationView.canShowCallout = true;
    }

    return annotationView;
}

How i can accomplish these task?From this link you can download sample project.https://drive.google.com/file/d/0B5pNDpbvZ8SnRExkamtmdkwzeWc/view?usp=sharing


Solution

  • Use this extension of mapKit, and adjust the values as you need, if the values is lower the the zoom is greater

    EDITED

    OBJECTIVE-C

    .h

    #import <MapKit/MapKit.h>
    
    @interface MKMapView (Zoom)
    
    -(void)zoomToUserLocation;
    
    -(void)zoomToUserLocationWith:(CLLocationCoordinate2D)coordinate and:(CLLocationDistance)latitudinalMeters and:(CLLocationDistance)longitudinalMeters;
    
    -(void)zoomToUserLocationWith:(CLLocationDistance)latitudinalMeters and:(CLLocationDistance)longitudinalMeters;
    
    @end
    

    .m

    #import "MKMapView+Zoom.h"
    
    @implementation MKMapView (Zoom)
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    */
    
    -(void)zoomToUserLocation
    {
        [self zoomToUserLocationWith:1000 and:1000];
    }
    
    -(void)zoomToUserLocationWith:(CLLocationCoordinate2D)coordinate and:(CLLocationDistance)latitudinalMeters and:(CLLocationDistance)longitudinalMeters
    {
        [self setRegion:MKCoordinateRegionMakeWithDistance(coordinate, latitudinalMeters, longitudinalMeters)];
    }
    
    -(void)zoomToUserLocationWith:(CLLocationDistance)latitudinalMeters and:(CLLocationDistance)longitudinalMeters
    {
        if(self.userLocation.location != nil){
            [self zoomToUserLocationWith:self.userLocation.location.coordinate and:latitudinalMeters and:longitudinalMeters];
        }
    }
    @end
    

    use it

    [self.mapView zoomToUserLocation];
    

    or

    [self.mapView zoomToUserLocationWith:50 and:50];
    

    or you can use it in

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
        [mapView zoomToUserLocationWith:view.annotation.coordinate and:500 and:500];
    }
    

    SWIFT

      extension MKMapView {
      func zoomToUserLocation() {
         self.zoomToUserLocation(latitudinalMeters: 1000, longitudinalMeters: 1000)
      }
    
      func zoomToUserLocation(latitudinalMeters:CLLocationDistance,longitudinalMeters:CLLocationDistance)
      {
        guard let coordinate = userLocation.location?.coordinate else { return }
        let region = MKCoordinateRegionMakeWithDistance(coordinate, latitudinalMeters, longitudinalMeters)
        setRegion(region, animated: true)
      }
    
    }
    

    Use it

    mapView.zoomToUserLocation()
    

    or

    mapView.zoomToUserLocation(latitudinalMeters:50,longitudinalMeters:50)
    

    Hope this helps