Obtener la ubicación / coordenadas actuales del usuario

194

¿Cómo puedo almacenar la ubicación actual del usuario y también mostrar la ubicación en un mapa?

Puedo mostrar coordenadas predefinidas en un mapa, pero no sé cómo recibir información del dispositivo.

También sé que tengo que agregar algunos elementos a una lista. ¿Cómo puedo hacer eso?

Awais Hussain
fuente

Respuestas:

225

Para obtener la ubicación actual de un usuario, debe declarar:

let locationManager = CLLocationManager()

En viewDidLoad()tienes que instanciar la CLLocationManagerclase, así:

// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization() 

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

if CLLocationManager.locationServicesEnabled() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.startUpdatingLocation()
}

Luego, en el método CLLocationManagerDelegate, puede obtener las coordenadas de ubicación actuales del usuario:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
    print("locations = \(locValue.latitude) \(locValue.longitude)")
}

En la lista de información, tendrá que agregar NSLocationAlwaysUsageDescription y su mensaje de alerta personalizado como; AppName (aplicación de demostración) desea utilizar su ubicación actual.

Annu
fuente
6060
No se olvide de agregar Import MapKit+ CoreLocation+ CLLocationManagerDelegateen la definición de clase.
Lukesivi
77
@Yusha Si esto te parece Objective-C, entonces nunca has visto Objective-C (ni Swift)
Martin Marconcini
44
Olvidó mencionar la implementación del protocolo CLLocationManagerDelegate.
ssd352
13
NSLocationAlwaysUsageDescriptionha cambiado su nombre aPrivacy - Location Always Usage Description
Saoud Rizwan
44
Debe declarar locationManagercomo una variable global en lugar de una variable local enviewDidLoad
chengsam
100

debes hacer esos pasos:

  1. agregar CoreLocation.frameworka BuildPhases -> Link Binary With Libraries (ya no es necesario a partir de XCode 7.2.1)
  2. importar CoreLocationa su clase, muy probablemente ViewController.swift
  3. agregar CLLocationManagerDelegatea su declaración de clase
  4. agregar NSLocationWhenInUseUsageDescriptiony NSLocationAlwaysUsageDescriptionplist
  5. Administrador de ubicación de init:

    locationManager = CLLocationManager()
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()
  6. obtener la ubicación del usuario por:

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
        print("locations = \(locValue.latitude) \(locValue.longitude)")
    }
itzhar
fuente
3
nueva función: func locationManager (gerente: CLLocationManager, didUpdateLocations ubicaciones: [CLLocation])
user523234
Paso a paso la respuesta. Pero por favor actualízalo. No está funcionando por ahora.
Dheeraj D
59

Actualización para iOS 12.2 con Swift 5

debe agregar los siguientes permisos de privacidad en el archivo plist

<key>NSLocationWhenInUseUsageDescription</key>
<string>Description</string>

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Description</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>Description</string>

Así es como soy

obtener la ubicación actual y mostrarla en el Mapa en Swift 2.0

Asegúrese de haber agregado el marco CoreLocation y MapKit a su proyecto (esto no es necesario con XCode 7.2.1)

import Foundation
import CoreLocation
import MapKit

class DiscoverViewController : UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var map: MKMapView!
    var locationManager: CLLocationManager!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        if (CLLocationManager.locationServicesEnabled())
        {
            locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()
            locationManager.startUpdatingLocation()
        }
    }

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

        let location = locations.last! as CLLocation

        let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

        self.map.setRegion(region, animated: true)
    }
}

Aquí está la pantalla de resultados.

una captura de pantalla del mapa, centrada en Mumbai

swiftBoy
fuente
Ejecuto su código y me sale una pantalla blanca en blanco. ¿Hay una vista o algo que necesito agregar al guión gráfico?
ThisClark
1
Me funcionó muy bien en Swift 4, solo tuve que agregar un guión bajo (solicitado por Xcode) para que la línea locationManager se convirtiera en: func locationManager (_ manager: CLLocationManager, didUpdateLocations ubicaciones: [CLLocation])
Elijah Lofgren
32

Importar biblioteca como:

import CoreLocation

establecer delegado:

CLLocationManagerDelegate

Tomar variables como:

var locationManager:CLLocationManager!

En viewDidLoad () escribe este bonito código:

 locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()

    if CLLocationManager.locationServicesEnabled(){
        locationManager.startUpdatingLocation()
    }

Escriba los métodos delegados de CLLocation:

    //MARK: - location delegate methods
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation :CLLocation = locations[0] as CLLocation

    print("user latitude = \(userLocation.coordinate.latitude)")
    print("user longitude = \(userLocation.coordinate.longitude)")

    self.labelLat.text = "\(userLocation.coordinate.latitude)"
    self.labelLongi.text = "\(userLocation.coordinate.longitude)"

    let geocoder = CLGeocoder()
    geocoder.reverseGeocodeLocation(userLocation) { (placemarks, error) in
        if (error != nil){
            print("error in reverseGeocode")
        }
        let placemark = placemarks! as [CLPlacemark]
        if placemark.count>0{
            let placemark = placemarks![0]
            print(placemark.locality!)
            print(placemark.administrativeArea!)
            print(placemark.country!)

            self.labelAdd.text = "\(placemark.locality!), \(placemark.administrativeArea!), \(placemark.country!)"
        }
    }

}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print("Error \(error)")
}

Ahora configure el permiso para acceder a la ubicación, así que agregue estos valores clave en su archivo info.plist

 <key>NSLocationAlwaysUsageDescription</key>
<string>Will you allow this app to always know your location?</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Do you allow this app to know your current location?</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Do you allow this app to know your current location?</string>

ingrese la descripción de la imagen aquí

100% trabajando sin ningún problema. Probado

Sr. Javed Multani
fuente
Tengo otro idioma, entonces no funciona. Solo quiero el idioma inglés.
Maulik shah
29

@wonderwhy He agregado la captura de pantalla de mi código.  por favor compruébalo.  Deberá agregar <code> NSLocationWhenInUseUsageDescription en la lista para autorización. </code>

NSLocationWhenInUseUsageDescription = Solicitar permiso para usar el servicio de ubicación cuando las aplicaciones están en segundo plano. en su archivo plist.

Si esto funciona, por favor vote la respuesta.

Annu
fuente
1
puede explicar su respuesta más agregando código. si lo desea
Krutarth Patel
16
sería bueno tener el fragmento de código usando lenguaje de marcado en lugar de pegar una captura de pantalla
Nicholas Allio
25

Primero importe la biblioteca Corelocation y MapKit:

import MapKit
import CoreLocation

heredar de CLLocationManagerDelegate a nuestra clase

class ViewController: UIViewController, CLLocationManagerDelegate

crea una variable locationManager, esta será tu información de ubicación

var locationManager = CLLocationManager()

cree una función para obtener la información de ubicación, sea específico, esta sintaxis exacta funciona:

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

en su función, cree una ubicación actual constante para los usuarios

let userLocation:CLLocation = locations[0] as CLLocation // note that locations is same as the one in the function declaration  

deje de actualizar la ubicación, esto evita que su dispositivo cambie constantemente la ventana para centrar su ubicación mientras se mueve (puede omitir esto si desea que funcione de otra manera)

manager.stopUpdatingLocation()

haga que los usuarios coordinen desde userLocatin que acaba de definir:

let coordinations = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude,longitude: userLocation.coordinate.longitude)

defina qué tan ampliado desea que sea su mapa:

let span = MKCoordinateSpanMake(0.2,0.2) combina estos dos para obtener la región:

let region = MKCoordinateRegion(center: coordinations, span: span)//this basically tells your map where to look and where from what distance

ahora configure la región y elija si desea que vaya allí con animación o no

mapView.setRegion(region, animated: true)

cierra tu función }

desde su botón u otra forma en que desee establecer locationManagerDeleget en self

ahora permita que se muestre la ubicación

designar precisión

locationManager.desiredAccuracy = kCLLocationAccuracyBest

autorizar:

 locationManager.requestWhenInUseAuthorization()

para poder autorizar el servicio de ubicación, debe agregar estas dos líneas a su lista

ingrese la descripción de la imagen aquí

obtener ubicación:

locationManager.startUpdatingLocation()

muéstralo al usuario:

mapView.showsUserLocation = true

Este es mi código completo:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!

    var locationManager = CLLocationManager()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func locateMe(sender: UIBarButtonItem) {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

        mapView.showsUserLocation = true

    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0] as CLLocation

        manager.stopUpdatingLocation()

        let coordinations = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude,longitude: userLocation.coordinate.longitude)
        let span = MKCoordinateSpanMake(0.2,0.2)
        let region = MKCoordinateRegion(center: coordinations, span: span)

        mapView.setRegion(region, animated: true)

    }
}
MiladiuM
fuente
20

Swift 3.0

Si no desea mostrar la ubicación del usuario en el mapa, pero solo desea almacenarla en Firebase o en otro lugar, siga estos pasos,

import MapKit
import CoreLocation

Ahora use CLLocationManagerDelegate en su VC y debe anular los últimos tres métodos que se muestran a continuación. Puede ver cómo el método requestLocation () le proporcionará la ubicación actual del usuario utilizando estos métodos.

class MyVc: UIViewController, CLLocationManagerDelegate {

  let locationManager = CLLocationManager()

 override func viewDidLoad() {
    super.viewDidLoad()

    isAuthorizedtoGetUserLocation()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    }
}

 //if we have no permission to access user location, then ask user for permission.
func isAuthorizedtoGetUserLocation() {

    if CLLocationManager.authorizationStatus() != .authorizedWhenInUse     {
        locationManager.requestWhenInUseAuthorization()
    }
}


//this method will be called each time when a user change his location access preference.
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if status == .authorizedWhenInUse {
        print("User allowed us to access location")
        //do whatever init activities here.
    }
}


 //this method is called by the framework on         locationManager.requestLocation();
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print("Did location updates is called")
    //store the user location here to firebase or somewhere 
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print("Did location updates is called but failed getting location \(error)")
}

}

Ahora puede codificar la siguiente llamada una vez que el usuario inicie sesión en su aplicación. Cuando se invoca requestLocation (), invocará didUpdateLocations anteriormente y podrá almacenar la ubicación en Firebase o en cualquier otro lugar.

if CLLocationManager.locationServicesEnabled() {
            locationManager.requestLocation();
 }

si está utilizando GeoFire, en el método didUpdateLocations anterior puede almacenar la ubicación de la siguiente manera

geoFire?.setLocation(locations.first, forKey: uid) where uid is the user id who logged in to the app. I think you will know how to get UID based on your app sign in implementation. 

Por último, pero no menos importante, vaya a su Info.plist y habilite "Privacidad-Ubicación cuando esté en uso Descripción del uso".

Cuando usa el simulador para probarlo, siempre le da una ubicación personalizada que configuró en Simulador -> Depurar -> Ubicación.

Lyju I Edwinson
fuente
Hola, ¿Dónde están las ubicaciones (longitud, latitud), ¿Con qué frecuencia se recargan las ubicaciones?
Cristian Mora
@CristianMora cuando se invoca locationManager.requestLocation, se invoca locationManager (_ manager: CLLocationManager, didUpdateLocations ubicaciones: [CLLocation]) donde se trata de una matriz de ubicaciones y puede usar una de ellas o mostrarle al usuario que elija la más adecuada. ubicaciones es de tipo CLLocation y puede obtener longitud, latitud de este objeto. Si necesita la información del usuario solo una vez, no necesita recargar ubicaciones; de lo contrario, puede llamar a requestLocation () según sea necesario. Por ejemplo, una solicitud de búsqueda, primero llama a requestLocation () y luego, según la ubicación, da respuestas.
Lyju I Edwinson
15

primero agregue dos marcos en su proyecto

1: MapKit

2: Corelocation (ya no es necesario a partir de XCode 7.2.1)

Define en tu clase

var manager:CLLocationManager!
var myLocations: [CLLocation] = []

luego en viewDidLoad código de método esto

manager = CLLocationManager()
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestAlwaysAuthorization()
manager.startUpdatingLocation()

//Setup our Map View
mapobj.showsUserLocation = true

no olvide agregar estos dos valores en el archivo plist

1: NSLocationWhenInUseUsageDescription

2: NSLocationAlwaysUsageDescription
Krutarth Patel
fuente
11
import CoreLocation
import UIKit

class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager: CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
    } 

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status != .authorizedWhenInUse {return}
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
        let locValue: CLLocationCoordinate2D = manager.location!.coordinate
        print("locations = \(locValue.latitude) \(locValue.longitude)")
    }
}

Debido a que la llamada a requestWhenInUseAuthorizationes asíncrona, la aplicación llama a la locationManagerfunción después de que el usuario le haya otorgado permiso o lo haya rechazado. Por lo tanto, es apropiado colocar su ubicación obteniendo código dentro de esa función dado el permiso otorgado por el usuario. Este es el mejor tutorial sobre esto que he encontrado en él .

ScottyBlades
fuente
10
 override func viewDidLoad() {

    super.viewDidLoad()       
    locationManager.requestWhenInUseAuthorization();     
    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }
    else{
        print("Location service disabled");
    }
  }

Este es su método de carga de la vista y en la clase ViewController también incluye el método de actualización mapStart de la siguiente manera

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    var locValue : CLLocationCoordinate2D = manager.location.coordinate;
    let span2 = MKCoordinateSpanMake(1, 1)    
    let long = locValue.longitude;
    let lat = locValue.latitude;
    print(long);
    print(lat);        
    let loadlocation = CLLocationCoordinate2D(
                    latitude: lat, longitude: long            

   )     

    mapView.centerCoordinate = loadlocation;
    locationManager.stopUpdatingLocation();
}

Además , no olvide agregar CoreLocation.FrameWork y MapKit.Framework en su proyecto (ya no es necesario a partir de XCode 7.2.1 )

Tek Raj
fuente
6

Uso:

Definir campo en clase

let getLocation = GetLocation()

Uso en función de clase por código simple:

getLocation.run {
    if let location = $0 {
        print("location = \(location.coordinate.latitude) \(location.coordinate.longitude)")
    } else {
        print("Get Location failed \(getLocation.didFailWithError)")
    }
}

Clase:

import CoreLocation

public class GetLocation: NSObject, CLLocationManagerDelegate {
    let manager = CLLocationManager()
    var locationCallback: ((CLLocation?) -> Void)!
    var locationServicesEnabled = false
    var didFailWithError: Error?

    public func run(callback: @escaping (CLLocation?) -> Void) {
        locationCallback = callback
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
        manager.requestWhenInUseAuthorization()
        locationServicesEnabled = CLLocationManager.locationServicesEnabled()
        if locationServicesEnabled { manager.startUpdatingLocation() }
        else { locationCallback(nil) }
    }

   public func locationManager(_ manager: CLLocationManager,
                         didUpdateLocations locations: [CLLocation]) {
        locationCallback(locations.last!)
        manager.stopUpdatingLocation()
    }

    public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        didFailWithError = error
        locationCallback(nil)
        manager.stopUpdatingLocation()
    }

    deinit {
        manager.stopUpdatingLocation()
    }
}


No olvide agregar "NSLocationWhenInUseUsageDescription" en la lista de información.

Renetik
fuente
1
¡Gracias! No se olvide de agregar "NSLocationWhenInUseUsageDescription" en la lista de información
Jonas Deichelmann
2
import Foundation
import CoreLocation

enum Result<T> {
  case success(T)
  case failure(Error)
}

final class LocationService: NSObject {
private let manager: CLLocationManager

init(manager: CLLocationManager = .init()) {
    self.manager = manager
    super.init()
    manager.delegate = self
}

var newLocation: ((Result<CLLocation>) -> Void)?
var didChangeStatus: ((Bool) -> Void)?

var status: CLAuthorizationStatus {
    return CLLocationManager.authorizationStatus()
}

func requestLocationAuthorization() {
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    if CLLocationManager.locationServicesEnabled() {
        manager.startUpdatingLocation()
        //locationManager.startUpdatingHeading()
    }
}

func getLocation() {
    manager.requestLocation()
}

deinit {
    manager.stopUpdatingLocation()
}

}

 extension LocationService: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    newLocation?(.failure(error))
    manager.stopUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let location = locations.sorted(by: {$0.timestamp > $1.timestamp}).first {
        newLocation?(.success(location))
    }
      manager.stopUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    switch status {
    case .notDetermined, .restricted, .denied:
        didChangeStatus?(false)
    default:
        didChangeStatus?(true)
    }
}
}

Necesita escribir este código en el ViewController requerido.

 //NOTE:: Add permission in info.plist::: NSLocationWhenInUseUsageDescription


let locationService = LocationService()

 @IBAction func action_AllowButtonTapped(_ sender: Any) {
  didTapAllow()
 }

 func didTapAllow() {
   locationService.requestLocationAuthorization()
 }

 func getCurrentLocationCoordinates(){
   locationService.newLocation = {result in
     switch result {
      case .success(let location):
      print(location.coordinate.latitude, location.coordinate.longitude)
      case .failure(let error):
      assertionFailure("Error getting the users location \(error)")
   }
  }
}

    func getCurrentLocationCoordinates(){
    locationService.newLocation = {result in
        switch result {
        case .success(let location):
            print(location.coordinate.latitude, location.coordinate.longitude)
            CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
                if error != nil {
                    print("Reverse geocoder failed with error" + (error?.localizedDescription)!)
                    return
                }
                if (placemarks?.count)! > 0 {
                    print("placemarks", placemarks!)
                    let pmark = placemarks?[0]
                    self.displayLocationInfo(pmark)
                } else {
                    print("Problem with the data received from geocoder")
                }
            })
        case .failure(let error):
            assertionFailure("Error getting the users location \(error)")
        }
    }
}
Mannam Brahmam
fuente
0

Aquí hay un ejemplo de copiar y pegar que funcionó para mí.

http://swiftdeveloperblog.com/code-examples/determine-users-current-location-example-in-swift/

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager:CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        determineMyCurrentLocation()
    }


    func determineMyCurrentLocation() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.startUpdatingLocation()
            //locationManager.startUpdatingHeading()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0] as CLLocation

        // Call stopUpdatingLocation() to stop listening for location updates,
        // other wise this function will be called every time when user location changes.

       // manager.stopUpdatingLocation()

        print("user latitude = \(userLocation.coordinate.latitude)")
        print("user longitude = \(userLocation.coordinate.longitude)")
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
    {
        print("Error \(error)")
    }
}
Eric Stevenson
fuente
-1
// its with strongboard 
@IBOutlet weak var mapView: MKMapView!

//12.9767415,77.6903967 - exact location latitude n longitude location 
let cooridinate = CLLocationCoordinate2D(latitude: 12.9767415 , longitude: 77.6903967)
let  spanDegree = MKCoordinateSpan(latitudeDelta: 0.2,longitudeDelta: 0.2)
let region = MKCoordinateRegion(center: cooridinate , span: spanDegree)
mapView.setRegion(region, animated: true)
Ramanaraynan
fuente
-1

100% trabajando en iOS Swift 4 por: Parmar Sajjad

Paso 1: Vaya a la consola Api de GoogleDeveloper y cree su ApiKey

Paso 2: Goto Project instala el pod de Cocoapods GoogleMaps

paso 3: Goto AppDelegate.swift importa GoogleMaps y

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    GMSServices.provideAPIKey("ApiKey")
    return true
}

paso 4: importa UIKit importa la clase GoogleMaps ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var mapview: UIView!
let locationManager  = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    locationManagerSetting()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func locationManagerSetting() {

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    self.showCurrentLocationonMap()
    self.locationManager.stopUpdatingLocation()
}
func showCurrentLocationonMap() {
    let
    cameraposition  = GMSCameraPosition.camera(withLatitude: (self.locationManager.location?.coordinate.latitude)!  , longitude: (self.locationManager.location?.coordinate.longitude)!, zoom: 18)
    let  mapviewposition = GMSMapView.map(withFrame: CGRect(x: 0, y: 0, width: self.mapview.frame.size.width, height: self.mapview.frame.size.height), camera: cameraposition)
    mapviewposition.settings.myLocationButton = true
    mapviewposition.isMyLocationEnabled = true

    let marker = GMSMarker()
    marker.position = cameraposition.target
    marker.snippet = "Macczeb Technologies"
    marker.appearAnimation = GMSMarkerAnimation.pop
    marker.map = mapviewposition
    self.mapview.addSubview(mapviewposition)

}

}

Paso 5: abra el archivo info.plist y agregue a continuación Privacidad: ubicación cuando está en uso Descripción del uso ...... debajo del nombre base del archivo del guión gráfico principal

paso 6: corre

Sajjad Parmar
fuente