Как получить текущее местоположение GPS в приложении для iOS без использования раскадровки - PullRequest
0 голосов
/ 17 октября 2019

Я хочу установить или отслеживать текущее местоположение пользователя автоматически в swift 4 ИЛИ Как сохранить текущее местоположение пользователя?

1 Ответ

0 голосов
/ 04 ноября 2019

Сначала добавьте эти свойства в info.plist

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>description1</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>description2</string>
    <key>NSLocationUsageDescription</key>
    <string>description3</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>accept to get location</string>
    <key>UIBackgroundModes</key>

А затем после копирования этого контроллера вида в вашем проекте

import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {

 var locationManager:CLLocationManager!
    override func viewDidLoad() {
        super.viewDidLoad()
        determineMyCurrentLocation()
        // Do any additional setup after loading the view.
    }


    func determineMyCurrentLocation() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager!.allowsBackgroundLocationUpdates = true
        locationManager!.pausesLocationUpdatesAutomatically = false
        let value =   locationManager.startMonitoringSignificantLocationChanges()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.startUpdatingLocation()
        }
    }
    func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
         print("ankur :- \(region)")
    }
    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)")
    }

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

}

Если есть запрос, прокомментируйте его

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...