Go to/zoom to current location function (MapKit)
Asked Answered
W

5

9

I've got a mapView which zooms to the current location using viewDidLoad :

#define METERS_PER_MILE 1609.344

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    mapView.showsUserLocation=TRUE;

    // zoom to  a specific area
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = -28.994167;
    zoomLocation.longitude = 134.866944;

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 1900*METERS_PER_MILE, 1900*METERS_PER_MILE);
    MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];    

    // make sure the Google water mark is always visible
    mapView.autoresizingMask =
    (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

    [mapView setRegion:adjustedRegion animated:YES];        

    mapView.delegate=self;

    searchBar.delegate = self;
}

This works fine. I've added a search bar and a function to jump to a specific address location. This works fine, too. I now want to add a button to jump back to the current location. Can you give me a hand, please?

Cheers

Warfare answered 11/8, 2011 at 6:10 Comment(0)
C
11

You need to set the center of your map to the current location on tap of that button. Say, like this:

- (IBAction)showCurrentLocation {        
    [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
}
Counterword answered 11/8, 2011 at 6:35 Comment(3)
is there a function called setCenter?Arrowy
[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];Arrowy
@JohannesRudolph no worries!Arrowy
F
2

you can also try:

mapView.userTrackingMode=YES;
mapView.userTrackingMode=NO;
Fanchon answered 3/2, 2013 at 10:55 Comment(2)
Just make sure to call this in the viewDidAppear method or later.Bowfin
userTrackingMode is not a BOOL. It's a MKUserTrackingMode enum. Only reason setting it to YES/NO "works" is because YES is equal to MKUserTrackingModeFollow and NO is equal to MKUserTrackingModeNone. But userTrackingMode can also be a third value MKUserTrackingModeFollowWithHeading.Amylaceous
P
1

You can link this IBAction to your UIButton, it's going to move the map on the current location and zoom on it.

@IBOutlet weak var mapView: MKMapView!

@IBAction func zoomToUserCurrentLocation(sender: AnyObject) {
    if self.mapView != nil {
        self.mapView.setRegion(MKCoordinateRegionMake(
            self.mapView.userLocation.coordinate, 
            MKCoordinateSpanMake(0.1, 0.1)
        ), animated: true)
    }
}

MKCoordinateSpan defines the area spanned by a map region, smaller these values are, closer you zoom on the map.

Panther answered 29/5, 2015 at 2:17 Comment(0)
C
0
- (void)showCurrentLocation{

    MKMapPoint annotationPoint = MKMapPointForCoordinate(self.mapView.userLocation.coordinate);
    MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.0, 0.0);
    [self.mapView setVisibleMapRect:zoomRect animated:YES];
}
Carlicarlick answered 27/7, 2014 at 12:54 Comment(0)
S
0

FOR SWIFT

Add this line in button action yourMKMapView.setUserTrackingMode(.follow, animated: true)

make sure you add yourMKMapView.showsUserLocation = true in viewDidLoad()

Scarify answered 21/7, 2017 at 2:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.