1
class ViewController: UIViewController {

var locationManager = CLLocationManager()

@IBOutlet weak var meuMapa: MKMapView!
override func viewDidLoad() {
    // Ask for Authorisation from the User.
    self.locationManager.requestAlwaysAuthorization()

    // For use in foreground
    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self //ERRO: Cannot assign value of type ‘ViewController’to type CLLocationManager
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()

Como pegar a localização do usuário Swift?

2 Respostas 2

0

Primeiramente você precisa adicionar o CLLocationManagerDelegate na sua classe ViewController:

class ViewController: UIViewController, CLLocationManagerDelegate {

Em seguida implementar o método didUpdateLocations:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.last!
    print("Latitude: \(location.coordinate.latitude)")
    print("Longitude: \(location.coordinate.longitude)")
}
0

Neste e o método de usuário saber a sua localização

import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBAction func LocalizaME (_ sender: AnyObject) {

        Manager.delegate = self
        Manager.desiredAccuracy = kCLLocationAccuracyBest
        Manager.requestWhenInUseAuthorization()
        Manager.startUpdatingLocation()

        MapView.showsUserLocation = true

    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){

        let userlocation:CLLocation = locations [0] as CLLocation
        Manager.stopUpdatingLocation()

        let location = CLLocationCoordinate2D(latitude: userlocation.coordinate.latitude, longitude: userlocation.coordinate.longitude)

        let span = MKCoordinateSpanMake (0.5, 0.5)

        let region = MKCoordinateRegion(center: location, span: span)

        MapView.setRegion(region, animated: true)
    }
}

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .