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
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];
}
you can also try:
mapView.userTrackingMode=YES;
mapView.userTrackingMode=NO;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With