How to show my current position and region onto my MKMapView with using blue dots ?

StackOverflow https://stackoverflow.com/questions/23559974

  •  18-07-2023
  •  | 
  •  

Question

I am trying to create the dummy map view demo for iOS and storyboard allowing current user location to be acquired. When it comes to execution, I have to scroll and swipe to find my current location but not being set at the region nearby my current location. What should I implement in order to achieve this for instance using MK Coordinates or MKMap Region? the below is my .m implementation code.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [_mapView setCenterCoordinate:_mapView.userLocation.location.coordinate animated:YES];
    MKAnnotationView *userLocationView = [_mapView viewForAnnotation:_mapView.userLocation];
    [userLocationView.superview bringSubviewToFront:userLocationView];
}
Was it helpful?

Solution

MAPKIT

First allow your map to show your current location.

 _mapView.showsUserLocation   =   YES; // This will show the blue dot on map at your current location.

Now Set the Region to zoom on your current location.

MKCoordinateRegion region;
    region.center.latitude           =      _mapView.userLocation.coordinate.latitude;;
    region.center.longitude          =      _mapView.userLocation.coordinate.longitude;;

    region.span.latitudeDelta        =      0.001;
    region.span.longitudeDelta       =      0.001;

    MKCoordinateRegion scaledRegion  =       [_mapView regionThatFits:region];
    [_mapView setRegion:scaledRegion animated:NO];

Make Sure to set the Region after your location is visible over map or your map is completly loaded. You can make use to delegates..

- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView;
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView;

If you want to manage the UserLocation by userself.. You can use CLLocationManager class Initialize it and Use it's Delegate to get the updates on User's Current location.

CLLocationManager *locationManager  =           [[CLLocationManager alloc]init];
locationManager.desiredAccuracy     =           kCLLocationAccuracyBest;
locationManager.pausesLocationUpdatesAutomatically = NO;
[locationManager setDelegate:self];

// Delegate

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// Use New Location to show your custom marker or do any thing you want.
}

For Debugging in Simulator you set the GPS location Go to Simulator option DEBUG-->LOCATION .. You can add custom location or can chose from the available ones.

Hope this will help you. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top