Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKMapView center and zoom in

I am using MKMapView on a project and would like to center the map on a coordinate and zoom in. Just like Google maps has:

GMSCameraPosition.camera(withLatitude: -33.8683,
                                  longitude: 151.2086,
                                  zoom: 6)

Is there any Mapkit method for this?

like image 633
apinho Avatar asked Jan 13 '17 16:01

apinho


3 Answers

Here is a method I use to center your map on a pre-defined CLLocation using MKCoordinateRegion.

func centerMapOnLocation(_ location: CLLocation, mapView: MKMapView) {
    let regionRadius: CLLocationDistance = 1000
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
        regionRadius * 2.0, regionRadius * 2.0)
    mapView.setRegion(coordinateRegion, animated: true)
}
like image 118
Pierce Avatar answered Nov 01 '22 07:11

Pierce


You'd create a MKCoordinateRegion object and set that as the region on your MKMapView object.

MKCoordinateRegion mapRegion;   
CLLocationCoordinate2D coordinate;
coordinate.latitude = 0;
coordinate.longitude = 0;    
mapRegion.center = coordinate;
mapRegion.span.latitudeDelta = 0.2;
mapRegion.span.longitudeDelta = 0.2;

[mapView setRegion:mapRegion animated: YES];
like image 9
mattsson Avatar answered Nov 01 '22 06:11

mattsson


Put the below code in your CustomMapView subclass of MKMapView Call is from init

class CustomMapView: MKMapView {
private func zoom() {
            let dortmundLocation = CLLocation(latitude: 51.516667, longitude: 7.466667)
            let dortmunRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: dortmundLocation.coordinate.latitude, longitude: dortmundLocation.coordinate.longitude), span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
            self.setRegion(dortmunRegion, animated: true)
        }
}
like image 6
Wasim Avatar answered Nov 01 '22 08:11

Wasim