Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKMapView zoom to users location on viewDidLoad?

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];    } 
like image 403
buzzkip Avatar asked May 19 '11 19:05

buzzkip


People also ask

How does mkmapviewdelegate work?

The map view uses the data in each overlay object to determine when the corresponding overlay view needs to appear onscreen. When an overlay moves onscreen, the map view asks its delegate to create a corresponding overlay renderer. var delegate: MKMapViewDelegate? The receiver’s delegate.

How do I use mapboxmapview in SwiftUI?

The MapBoxMapView can then be used inside any SwiftUI view hierarchy, for example inside the default ContentView.swift file that comes with any new SwiftUI project in Xcode. Upon launch, the map is loaded, then the user's location is acquired and the map flys to the latest user's position.

How does the map view work?

The map view uses the coordinate data in each annotation object to determine when the corresponding annotation view needs to appear onscreen. When an annotation moves onscreen, the map view asks its delegate to create a corresponding annotation view.

How to show the user's location on a Mapbox map?

We also explored how to add annotations to add annotaitons to a Mapbox map. Now, let's focus on showing the user's location on the map. To enable this functionality, we can leverage the built in features of the Mapbox Maps SDK that enables the app to observe and respond the user's location.


2 Answers

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]; } 
like image 93
Deepak Danduprolu Avatar answered Sep 18 '22 07:09

Deepak Danduprolu


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]; } 
like image 40
Abizern Avatar answered Sep 20 '22 07:09

Abizern