Esiste un pulsante dedicato in MapKit che centra la telecamera rispetto alla posizione dell'utente? O devo farlo manualmente creando il pulsante e attivare mapView.showsUserLocation = true
?Pulsante per centrare la vista sulla posizione dell'utente utilizzando MapKit
risposta
In questo modo funziona bene (Swift), ed è possibile personalizzare il pulsante:
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)
}
Una riga di codice NICE! – Jeff
È necessario prendere la coordinata della posizione dell'utente e impostare la regione.
- (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];
}
Soluzione 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)
Questo non è rapido, ma ok. Quindi, cosa stai dicendo è che devo creare manualmente il mio pulsante? Non esiste qualcosa del genere: 'mapView.enableButtonThatDoesCenterViewOverUser = true'. – Xernox
MKMapCamera servirà come punto di vista da cui stai osservando un punto su una mappa. –
Di cosa stai parlando? Non sto chiedendo come centrare la vista sull'utente manualmente - so come farlo. Mi chiedo se MapKit fornisca un pulsante build-in per farlo, come MapKit fornisce una piccola bussola per cambiare la direzione di visualizzazione verso Nord. – Xernox
Essere in ritardo, ahah, ma ho appena trovato la funzione: correttamente
CLLocationManager.requestLocation()
Quindi una volta che imposta il tuo locationManager (con requestAlwaysAuth orization) si può chiamare dove si vuole centrare la tua mappa sul proprio luogo :)
Possibile duplicato: http://stackoverflow.com/a/6170026/3051458 –
@Ramesh_T hai letto entrambe le domande? Non voglio solo centrare la videocamera rispetto all'utente, voglio un pulsante che lo faccia, proprio come in Google/Apple Maps – Xernox