tutorial current annotation ios swift mapkit mapkitannotation

ios - annotation - swift 4 get current location



Agregando Pines al Mapa usando MapKit-Swift 3.0 (2)

Nuevo codificador, tratando de averiguar cómo usar MapKit. El objetivo es crear un mapa en el que los usuarios puedan agregar pins para utilizar su dirección. Sin embargo, en el paso en el que me encuentro ahora, tengo problemas para descubrir cómo agregar pines al mapa.

¿Cómo puedo añadir un pin al mapa? He estado luchando para averiguar cómo usar anotaciones hasta ahora.

Eso es con lo que espero ayuda / dirección. ¡Gracias!

import UIKit import MapKit import CoreLocation class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { @IBOutlet weak var bigMap: MKMapView! let locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() self.locationManager.delegate = self self.locationManager.desiredAccuracy = kCLLocationAccuracyBest self.locationManager.requestWhenInUseAuthorization() self.locationManager.startUpdatingLocation() self.bigMap.showsUserLocation = true } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let location = locations.last let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)) self.bigMap.setRegion(region, animated: true) self.locationManager.stopUpdatingLocation() } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print("Errors " + error.localizedDescription) } }


La forma en que aprendí a hacer esto es:

  1. En la misma función que viewDidLoad (), coloque las siguientes líneas de código:

    let annotation = MKPointAnnotation() annotation.title = "Your text here" //You can also add a subtitle that displays under the annotation such as annotation.subtitle = "One day I''ll go here..." annotation.coordinate = center

Este es el único lugar donde puedo ver dónde tiene una coordenada si todo lo demás falla, solo agregue la coordenada (busque en Google si
necesita saber cómo crear una coordenada) y establecerla igual a la
variable "annotation.coordinate"

map.addAnnotation(annotation)

  1. Dassit!

Se llaman annotations en el MapKit y las creas así:

let annotation = MKPointAnnotation()

Luego, en el método viewDidLoad() , simplemente configure las coordenadas y agréguelas al mapa como:

annotation.coordinate = CLLocationCoordinate2D(latitude: 11.12, longitude: 12.11) mapView.addAnnotation(annotation)

Los números son tus coordenadas.