繁体   English   中英

如何将来自用户设备的蓝点更改为来自 API 的精确位置纬度和经度

[英]How to change the blue dot coming from users device to precise location latitude and longtitude coming from API

我正在制作一个应用程序来跟踪车辆,我在使蓝点出现在来自 locationManager function 的纬度和经度上时遇到问题,但蓝点显示模拟器位置,我想用 API 分配蓝点它的纬度和经度不是模拟器或用户设备的位置,而是车辆。

This is the code for the Location, the fetchAndReloadData() is the function that calls the longitude and latitude from API structure and is related to the function locationManager below it, but the centerViewOnUserLocation() contains the blue dot but its showing it to somewhere else这是来自这个声明? :如果让 location = locationManager.location?.coordinate,现在如何将其更改为我的纬度和经度以显示蓝点:)。

globalVehicle = Vehicles ---> globalVehicle 包含来自 APIManager fetchAndReloadData() 的 API ---> 它告诉 API 到 Z99938282F04071859941E18F16EFCF42 的位置

我希望我没有让它难以理解,如果是这样,请病态解释一切:))

class MapViewController: UIViewController, MKMapViewDelegate {

var globalVechicle = [Vehicles]()
var id = "6438367CC43848B497FE4604AF465D6A"

let locationManager = CLLocationManager()
let regionInMeters: Double = 10000

@IBOutlet weak var mapView: MKMapView!

@IBAction func changeMapType(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        mapView.mapType = .standard
    }else {
        mapView.mapType = .satellite
    }
}


@IBAction func closeButton(_ sender: Any) {
    dismiss(animated: true, completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()
    checkLocationServices()
}


func setupLocationManager() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
}


func checkLocationServices() {
    if CLLocationManager.locationServicesEnabled() {
        setupLocationManager()
        checkLocationAuthorization()
    } else {
        print("Duhet ta leshoni lokacionin!")
    }
}


func checkLocationAuthorization() {
    switch CLLocationManager.authorizationStatus() {
    case .authorizedWhenInUse:
        fetchAndReloadData()
        mapView.showsUserLocation = true
        centerViewOnUserLocation()
        break
    case .denied:
        print("Duhet lejuar perdorimi lokacionit!")
        break
    case .notDetermined:
        locationManager.requestWhenInUseAuthorization()
    case .restricted:
        print("Nuk keni qasje te autorizuar per te leshuar lokacionin!")
        break
    case .authorizedAlways:
        break
    @unknown default:
        print("Error")
    }
}
}


extension MapViewController: CLLocationManagerDelegate {

func fetchAndReloadData(){
    APICaller.shared.getVehicles(for: id) {[weak self] (result) in
        guard let self = self else { return }
        
        switch result {
        case .success(let vehicle):
            self.globalVechicle = vehicle
            DispatchQueue.main.async {
                self.locationManager.startUpdatingLocation()
            }
        case .failure(let error):
            print(error)
        }
    }
}


func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let lattitude = globalVechicle[0].Latitude ,let longitude = globalVechicle[0].Longitude else { return }
    let carLocation = CLLocationCoordinate2D(latitude: lattitude , longitude: longitude)
    let region = MKCoordinateRegion.init(center: carLocation, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
    mapView.setRegion(region, animated: true)
    mapView.delegate = self
}


func centerViewOnUserLocation() {
    if let location = locationManager.location?.coordinate {
        let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
        mapView.setRegion(region, animated: true)
    }
}


func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    checkLocationAuthorization()
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM