0

Hola estoy implementando en mi proyecto de Xcode con swift 2 MapKit en el cual muestro un mapa con varios marcadores pero además necesito mostrar la ubicación actual del usuario, busque en varias páginas pero sin lograrlo.

import UIKit
import MapKit

class mapController: UIViewController {


    @IBOutlet weak var myMapView: MKMapView!


    override func viewDidLoad() {
        super.viewDidLoad()
        mapTajMahal()

    }

    func mapTajMahal()
    {
        //Taj Mahal Coordinates: 27.175015, 78.042139

        // Coordinates
        let tajLat:CLLocationDegrees = -39.849254
        let tajLong:CLLocationDegrees = -71.994215

        let tajCoordinate = CLLocationCoordinate2D(latitude: tajLat, longitude: tajLong)

        let mocho = CLLocationCoordinate2D(latitude: -39.849152, longitude: -71.943082)
        let aitue = CLLocationCoordinate2D(latitude: -39.849581, longitude: -71.945078)
        let rucapillan = CLLocationCoordinate2D(latitude: -39.833403841808995, longitude:-72.07239013854979)
        let c_elportal = CLLocationCoordinate2D(latitude: -39.848834,longitude: -71.937480)
        let c_los_alamos = CLLocationCoordinate2D(latitude: -39.851652, longitude: -71.931052)
        let c_lago_neltume = CLLocationCoordinate2D(latitude: -39.77999, longitude: -71.95708)
        let c_peumayen = CLLocationCoordinate2D(latitude: -39.848467, longitude: -71.938013)
        let domos = CLLocationCoordinate2D(latitude: -39.850835, longitude: -71.940623)
        let patagonia = CLLocationCoordinate2D(latitude: -39.858635, longitude: -71.918520)
        let madero = CLLocationCoordinate2D(latitude: -39.858635, longitude: -71.918520
        )

        //Span
        let latDelta:CLLocationDegrees = 0.2
        let longDelta:CLLocationDegrees = 0.2
        let tajSpan = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: longDelta)

        let tajRegion = MKCoordinateRegion(center: tajCoordinate, span: tajSpan)

        myMapView.setRegion(tajRegion, animated: true )

        let tajAnnotation = MKPointAnnotation()
        tajAnnotation.title = "Neltume"
        tajAnnotation.subtitle = "She walks in beauty"
        tajAnnotation.coordinate = tajCoordinate


        let mochoannotation = MKPointAnnotation()
        mochoannotation.title = "Mocho Neltume"
        mochoannotation.coordinate = mocho

        let aitueanotation = MKPointAnnotation()
        aitueanotation.title = "Aitue Expediciones"
        aitueanotation.coordinate = aitue

        let rucapillananotation = MKPointAnnotation()
        rucapillananotation.title = "Rafting Excursiones Restaurant Hostal Cabañas Rucapillan"
        rucapillananotation.coordinate = rucapillan

        let c_elportalanotation = MKPointAnnotation()
        c_elportalanotation.title = "Cabañas el Portal"
        c_elportalanotation.coordinate = c_elportal

        let c_los_alamosanotation = MKPointAnnotation()
        c_los_alamosanotation.title = "Cabañas los Alamos"
        c_los_alamosanotation.coordinate = c_los_alamos

        let c_lago_neltumeanotation = MKPointAnnotation()
        c_lago_neltumeanotation.title = "Cabañas Lago Neltume"
        c_lago_neltumeanotation.coordinate = c_lago_neltume

        let c_peumayenatonation = MKPointAnnotation()
        c_peumayenatonation.title = "Cabañas Peumayen"
        c_peumayenatonation.coordinate = c_peumayen

        let domosanotation = MKPointAnnotation()
        domosanotation.title = "Cabañas Karü Domos del Fuy"
        domosanotation.coordinate = domos

        let patagoniaanotation = MKPointAnnotation()
        patagoniaanotation.title = "Cabañas Patagonia Mawida"
        patagoniaanotation.coordinate = patagonia

        let maderoanotation = MKPointAnnotation()
        maderoanotation.title = "Restaurante Refugio del Madero"
        maderoanotation.coordinate = madero

        myMapView.addAnnotation(mochoannotation)
        myMapView.addAnnotation(aitueanotation)
        myMapView.addAnnotation(rucapillananotation)
        myMapView.addAnnotation(c_elportalanotation)
        myMapView.addAnnotation(c_los_alamosanotation)
        myMapView.addAnnotation(c_lago_neltumeanotation)
        myMapView.addAnnotation(c_peumayenatonation)
        myMapView.addAnnotation(patagoniaanotation)
        myMapView.addAnnotation(maderoanotation)

    }

}

2 respuestas 2

0

Los mapas (clase MKMapKit) tienen una propiedad showsUserLocation.

Alcanzaría con hacer

myMapView.showsUserLocation = true

Puedes ver la documentación (en inglés), también hay otras propiedades que te pueden interesar como ser userLocation o userTrackingMode.

1
  • Hola gracias por tu respuesta, por lo que vi el userlocation es para detectar la posición y locationvisible es para verla marcada en el mapa pero no lo logre hacerlo funcionar. el 23 ago. 2016 a las 19:38
0

Al final lo que hay que agregar es NSLocationWhenInUseUsageDescription en Info.plist. y en el Controller del mapa.

        myMapView.showsUserLocation = true


        myMapView.userLocationVisible; true


        self.locatinManager.delegate = self

        self.locatinManager.desiredAccuracy = kCLLocationAccuracyBest

        self.locatinManager.requestWhenInUseAuthorization()

        self.locatinManager.startUpdatingLocation()

        self.myMapView.showsUserLocation = true


    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: 2.2, longitudeDelta: 2.2))

        self.mapa2.setRegion(region, animated: true)

        self.locatinManager.stopUpdatingLocation()


        let mocho = CLLocationCoordinate2D(latitude: -39.848123, longitude: -71.940584)

        let mochoannotation = MKPointAnnotation()
        mochoannotation.title = "Neltume"
        mochoannotation.coordinate = mocho

         mapa2.addAnnotation(mochoannotation)
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("error: " + error.localizedDescription)
    }
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.