-->

How to zoom into user current location?

2019-08-06 04:09发布

问题:

I wanna zoom into the user current location when the app starts in the MapKit. This is my code (in the viewDidLoad function):

Locate=[[CLLocationManager alloc]init];
Locate.delegate=self;
Locate.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
Locate.distanceFilter=kCLDistanceFilterNone;
[Locate startUpdatingLocation];
[super viewDidLoad];

//Region
MKCoordinateRegion myRegion;

//Center
CLLocationCoordinate2D center;
center.latitude=Locate.location.coordinate.latitude;
center.longitude=Locate.location.coordinate.longitude;

//Span
MKCoordinateSpan span;
span.latitudeDelta=0.3f;
span.longitudeDelta=0.3f;
//Set Region
myRegion.center=center;
myRegion.span=span;
[_MyMap setRegion:myRegion animated:YES];

Also I implemented the didUpdateLocation function with the same code as previous. The problem is that when the user location is changing (when the user is moving) the screen makes zoom at him but I can't move the screen, if I try to move it return to the user location immediately, so I can't see the whole map.

回答1:

For Zooming the map you have to change the region values means center and span.once see this one Mapview Zoom

in my case i have used this one for moving the map when i click on particular button.

MKCoordinateSpan span = MKCoordinateSpanMake(30.5982f,0.0001f);
        CLLocationCoordinate2D coordinate = {36, 90};
        MKCoordinateRegion region = {coordinate, span};
        MKCoordinateRegion regionThatFits = [self.mapView regionThatFits:region];
        [self.mapView setRegion:regionThatFits animated:YES];


回答2:

I solved this issue by adding touchesMoved function and I stopped updating the Location in this function. Solved.