Is there a dedicated button in MapKit that centers camera over user location? Or do I have to do it manually creating button and toggle mapView.showsUserLocation = true
?
Asked
Active
Viewed 8,819 times
11

Xernox
- 1,706
- 1
- 23
- 37
-
Possible duplicate: http://stackoverflow.com/a/6170026/3051458 – Ramesh_T Jan 14 '16 at 14:58
-
3@Ramesh_T have you read both questions? I don't want just to center camera over user, I want a button that does that, just like in Google/Apple Maps – Xernox Jan 14 '16 at 15:03
-
@Xernox have you found any solution ? – Arshad Shaik Jul 19 '19 at 12:20
3 Answers
15
This way runs well (Swift), and you can customize the button:
class YourViewController{
...
@IBOutlet weak var mapView:MKMapView
...
override func viewDidLoad() {
super.viewDidLoad()
...
addMapTrackingButton()
}
func addMapTrackingButton(){
let image = UIImage(named: "trackme") as UIImage?
let button = UIButton(type: UIButtonType.System) as UIButton
button.frame = CGRectMake(5, 5, 35, 35)
button.setImage(image, forState: .Normal)
button.backgroundColor = .clearColor()
button.addTarget(self, action: #selector(YourViewController.centerMapOnUserButtonClicked), forControlEvents:.TouchUpInside)
self.mapView.addSubview(button)
}
func centerMapOnUserButtonClicked() {
self.mapView.setUserTrackingMode( MKUserTrackingMode.Follow, animated: true)
}
...
}
Swift 4:
func addMapTrackingButton(){
let image = UIImage(named: "trackme") as UIImage?
let button = UIButton(type: UIButtonType.custom) as UIButton
button.frame = CGRect(origin: CGPoint(x:5, y: 25), size: CGSize(width: 35, height: 35))
button.setImage(image, for: .normal)
button.backgroundColor = .clear
button.addTarget(self, action: #selector(ViewController.centerMapOnUserButtonClicked), for:.touchUpInside)
mapView.addSubview(button)
}
@objc func centerMapOnUserButtonClicked() {
mapView.setUserTrackingMode(MKUserTrackingMode.follow, animated: true)
}

Viker
- 3,183
- 2
- 25
- 25
3
Being late, ahah, but I just found the function :
CLLocationManager.requestLocation()
So once you properly set your locationManager (with requestAlwaysAuthorization) you can call it wherever you want to center your map on your own location :)

Adèle Dunand
- 78
- 7
-
Got error using this code, Delegate must respond to locationManager:didFailWithError: – Arshad Shaik Jul 19 '19 at 12:16
1
You need to take the user location coordinate and set the region.
- (IBAction)centerMapOnUserButtonClicked:(id)sender {
MKCoordinateSpan span = MKCoordinateSpanMake(0.0001f, 0.0001f);
CLLocationCoordinate2D coordinate = self.mapView.userLocation.coordinate;
MKCoordinateRegion region = {coordinate, span};
MKCoordinateRegion regionThatFits = [self.mapView regionThatFits:region];
NSLog(@"Fit Region %f %f", regionThatFits.center.latitude, regionThatFits.center.longitude);
[self.mapView setRegion:regionThatFits animated:YES];
}
Solution 2:
MKUserTrackingBarButtonItem *trackButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:mapView];
[trackButton setTarget:self];
[trackButton setAction:@selector(track:)];
[toolbar setItems:[NSArray arrayWithObjects:trackButton, nil] animated:YES];
Swift 4:
let trackButton = MKUserTrackingBarButtonItem.init(mapView: mapView)
trackButton.target = self
trackButton.action = #selector(ViewController.track)
toolbar.items?.append(trackButton)

Dmytro Skorokhod
- 424
- 8
- 17

Ramesh_T
- 1,251
- 8
- 19
-
That's not swift, but ok. So what are you saying is that I have to manually create my own button? There is no something like this: `mapView.enableButtonThatDoesCenterViewOverUser = true`. – Xernox Jan 14 '16 at 15:23
-
MKMapCamera will serve as the point of view you are observing a point on a map from. – Ramesh_T Jan 14 '16 at 15:27
-
3What are you talking about? I am not asking how to center view over user manually - I do know how to do it. I am asking does MapKit provide build in button to do that, like MapKit provides little compass to change viewing direction to North. – Xernox Jan 14 '16 at 15:36