Set a default location for the map to start at - i

2019-07-10 14:21发布

问题:

How do you set a default location for the map to start at? If I want to set it in Ireland for example, how would I do that? It currently defaults to around North America but I want it to default to a certain coordinates.

回答1:

The below code sets location to SFO. Change coordinates to your needs to set desired locations.

MKCoordinateRegion region = [mapView regionThatFits:MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(37.78275123, -122.40345442), 200, 200)];
[mapView setRegion:region animated:YES];

If you are looking for user's current location, use below code.

MKCoordinateRegion region = [mapView regionThatFits:MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 200, 200)];
[mapView setRegion:region animated:YES];


回答2:

Use setRegion:animated to set the initial region of the MKMapView:

-(void)viewDidLoad {

[super viewDidLoad];

self.mapView.delegate = self;

double delayInSeconds = 0.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
    region.center.latitude = 45.442424;
    region.center.longitude = -122.78;
    region.span.latitudeDelta =  0.60;
    region.span.longitudeDelta =  0.60;
    [mapView setRegion:region animated:YES];
});
}