MKMapView zoom to users location on viewDidLoad?

2020-05-14 03:56发布

I'm trying to zoom a map into the user's current location once the view loads, but I'm getting the error "** * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region '" when the view loads. Please can someone help?

Cheers!

- (void)viewDidLoad
{
    [super viewDidLoad];

    MKCoordinateRegion mapRegion;   
    mapRegion.center.latitude = map.userLocation.coordinate.latitude;
    mapRegion.center.longitude = map.userLocation.coordinate.longitude;
    mapRegion.span.latitudeDelta = 0.2;
    mapRegion.span.longitudeDelta = 0.2;
    [map setRegion:mapRegion animated: YES];   
}

10条回答
该账号已被封号
2楼-- · 2020-05-14 04:08

Here is @Deepak's answer in Swift 3 for iOS 10:

extension ViewController: MKMapViewDelegate
{
    func mapView( _ mapView: MKMapView, didUpdate userLocation: MKUserLocation )
    {
        let regionRadius = 400 // in meters
        let coordinateRegion = MKCoordinateRegionMakeWithDistance( userLocation.coordinate, regionRadius, regionRadius )
        self.mapView.setRegion( coordinateRegion, animated: true)
    }
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2020-05-14 04:09

As with Deepak's answer, except you could set the span more elegantly:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    MKCoordinateRegion mapRegion;   
    mapRegion.center = map.userLocation.coordinate;
    mapRegion.span = MKCoordinateSpanMake(0.2, 0.2);
    [map setRegion:mapRegion animated: YES];
}
查看更多
家丑人穷心不美
4楼-- · 2020-05-14 04:18

did you set showsUserLocation = YES? MKMapView won't update the location if it is set to NO. So make sure of that.

It is very likely that the MKMapView object doesn't have the user location yet. To do right, you should adopt MKMapViewDelegate protocol and implement mapView:didUpdateUserLocation:

map.delegate = self;

...
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{
    MKCoordinateRegion mapRegion;   
    mapRegion.center = mapView.userLocation.coordinate;
    mapRegion.span.latitudeDelta = 0.2;
    mapRegion.span.longitudeDelta = 0.2;

    [mapView setRegion:mapRegion animated: YES];
}
查看更多
霸刀☆藐视天下
5楼-- · 2020-05-14 04:19

You don't want to update this stuff inside userDidUpdateLocation if there is the possbility that the user will want to scroll the map. If you put that code in the mentioned method, the user will not be able to scroll the map because the function will be called and center the map back to the current location.

查看更多
叼着烟拽天下
6楼-- · 2020-05-14 04:21

The problem that occurs when you use the didUpdateUserLocation method, you will not be able to scroll nor zoom to another location. It will keep pointing to your location. The best way (that I've tried) is that you need to use the location manager.

In the viewDidLoad add the following :

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLLocationAccuracyKilometer;;
// showing user location with the blue dot
[self.mapView setShowsUserLocation:YES];

self.mapView.delegate = self;

// getting user coordinates
CLLocation *location = [_locationManager location];
CLLocationCoordinate2D  coordinate = [location coordinate];


// showing them in the mapView
_mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 250, 250);

[self.locationManager startUpdatingLocation];

this way you could display your location with the zoom that you want. I hope this will help.

查看更多
ゆ 、 Hurt°
7楼-- · 2020-05-14 04:24

......

MKCoordinateRegion region =mapView.region;

region.center.latitude = currentLocation.latitude ;
region.center.longitude = currentLocation.longitude;
region.span.longitudeDelta /= 1000.0;
region.span.latitudeDelta /= 1000.0;

[mapView setRegion:region animated:YES];
[mapView regionThatFits:region];
查看更多
登录 后发表回答