3

I have a method that works to move the the map so that it follows the current user location. This works fine, but currently the zoom scale doesn't work how I want. I would like the user to be able to zoom out or zoom in on the map how they want and still have it follow the current location at that NEW zoom scale that the user just set on the map.

I tried to do the following but it doesn't work well:

/**
 * Centers the map on the current location
 *
 * @version $Revision: 0.1
 */
- (void)centerMapOnLocation:(CLLocation *)location {

    // Make a region using our current zoom level
    CLLocationDistance latitude = mapView.region.span.latitudeDelta*100;
    CLLocationDistance longitude = mapView.region.span.longitudeDelta*100;
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location.coordinate, latitude, longitude);
    [mapView setRegion:region animated:YES];
}//end
Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412

1 Answers1

5

MKMapView Zoom and Region describes how to get the current view window.

When you're creating the new MKCoordinateRegion to set the new view to, use the constructor that allows you to specify the size:

UIKIT_STATIC_INLINE MKCoordinateRegion MKCoordinateRegionMake(
   CLLocationCoordinate2D centerCoordinate,
   MKCoordinateSpan span
);

You'll want to pass in the span from the current region and the centerCoordinate from the user location, which I'll take it you already got.

Community
  • 1
  • 1
  • Not sure what you mean. I can get the span from mapView.region.span so I have not need to make a new one. – Nic Hubbard Oct 10 '11 at 02:08
  • 1
    Exactly. But you need to make a new MKCoordinateRegion to set the mapView to, if you want to move it to the current user's location. But if you make it with MKCoordinateRegionMakeWithDistance, then the span is reset to some default value. Instead, make it with MKCoordinateRegionMake, and pass it the *current* span, so that the span doesn't change. –  Oct 10 '11 at 02:18
  • ah, I needed to use MKCoordinateRegionMake not MKCoordinateRegionMakeWithDistance. Thanks! – Nic Hubbard Oct 10 '11 at 02:21