0

I have a little problem. I just want to show the current user location at the center of the mapview. And the mapview zoom to the region that can show the annotation and the user location at the same time. The showAnnotation func of mapview seems to help,while the user location is not at the center of the mapview.

shadox
  • 997
  • 1
  • 8
  • 18
  • The area covered by the map view is the _region_. Setting the region is up to you. You get to specify the point you want at the center, and you get to specify the region. So show the user location as the center, but specify a region large enough to include the annotation. – matt Dec 08 '14 at 02:48
  • 1
    See http://stackoverflow.com/questions/26416587/fitting-annotations-on-a-mkmapview-while-keeping-user-position-centered –  Dec 08 '14 at 02:55

3 Answers3

1

You can use:

 _mapView.userTrackingMode  = MKUserTrackingModeFollow

MKUserTrackingMode will have the map follows the user's location

Gili Ariel
  • 572
  • 1
  • 6
  • 18
0
            var userLoc = self.mapView.userLocation

            let newDistance = CLLocation(latitude: userLoc.coordinate.latitude, longitude: userLoc.coordinate.longitude).distanceFromLocation(CLLocation(latitude: annotation.latitude, longitude: annotation.longitude))
            let region = MKCoordinateRegionMakeWithDistance(userLoc.coordinate,2 * newDistance, 2 * newDistance)
            let adjustRegion = self.mapView.regionThatFits(region)
            self.mapView.setRegion(adjustRegion, animated:true)

The code above solved my problem.I just forget to multiplied by 2 to the distance. ...

shadox
  • 997
  • 1
  • 8
  • 18
0
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}

The above lines of code instructs the map view to zoom into a region that is 800 by 800 meters around the user’s location.

Saghir A. Khatri
  • 3,429
  • 6
  • 45
  • 76