gpt4 book ai didi

ios - 位置正常,但无法追踪我在 Swift 3 中行驶的距离

转载 作者:行者123 更新时间:2023-11-30 12:53:59 27 4
gpt4 key购买 nike

我正在为我的一门类(class)制作一个应用程序。它应该跟踪行驶的距离并更新标签以显示他们走了多远。当我打开应用程序时,它会请求跟踪位置的权限。 map View 可以工作,它跟随位置,但标签永远不会更新以显示行驶的距离。我在下面添加了我的代码,非常感谢任何帮助!

    //
// ViewController.swift
// location_tracker
//
// Created by Dale McCaughan on 2016-10-19.
// Copyright © 2016 Dale McCaughan. All rights reserved.
//

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

@IBOutlet weak var theMap: MKMapView!
@IBOutlet weak var l: UILabel!

let locationManager = CLLocationManager()
var startLocation: CLLocation!
var monitoredRegions: Dictionary<String, NSDate> = [:]

override func viewWillAppear(_ animated: Bool) {
self.navigationController?.isNavigationBarHidden = true
self.navigationController?.isToolbarHidden = false;

//Status bar style and visibility
UIApplication.shared.statusBarStyle = .lightContent

//Change status bar color
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
//if statusBar.respondsToSelector("setBackgroundColor:") {
statusBar.backgroundColor = UIColor.white
//}

UIToolbar.appearance().backgroundColor = UIColor.white

}

override func viewDidLoad() {
super.viewDidLoad()

//Setup the Location Manager
locationManager.delegate = self;
locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

//Setup the Map View
theMap.delegate = self
theMap.showsUserLocation = true
theMap.userTrackingMode = .follow

// setup test data

}

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

// status is not determined
if CLLocationManager.authorizationStatus() == .notDetermined {
locationManager.requestAlwaysAuthorization()
}
// authorization were denied
else if CLLocationManager.authorizationStatus() == .denied {
showAlert("Location services were previously denied. Please enable location services for this app in Settings.")
}
// we do have authorization
else if CLLocationManager.authorizationStatus() == .authorizedAlways {
locationManager.startUpdatingLocation()
}
}

// MARK: - MKMapViewDelegate

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let circleRenderer = MKCircleRenderer(overlay: overlay)
circleRenderer.strokeColor = UIColor.red
circleRenderer.lineWidth = 1.0
return circleRenderer
}

@IBAction func resetDistance(_ sender: AnyObject) {
startLocation = nil
}

// MARK: - CLLocationManagerDelegate

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
showAlert("enter \(region.identifier)")
monitoredRegions[region.identifier] = Date() as NSDate?
l.text = "in location manager1"
}

func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
showAlert("exit \(region.identifier)")
monitoredRegions.removeValue(forKey: region.identifier)
l.text = "in location manager2"
}

var lastLocation: CLLocation!
var traveledDistance:Double = 0

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

if let firstLocation = locations.first as? CLLocation
{
theMap.setCenter(firstLocation.coordinate, animated: true)

let region = MKCoordinateRegionMakeWithDistance(firstLocation.coordinate, 1000, 1000)
theMap.setRegion(region, animated: true)

if let oldLocation = lastLocation {
let delta: CLLocationDistance = firstLocation.distance(from: lastLocation)
traveledDistance += delta
}

lastLocation = firstLocation
}
l.text = String(format: "%.3f", traveledDistance/1000) + " kilometers"

}

// MARK: - Helpers

func showAlert(_ title: String) {
let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)

}

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

最佳答案

试试这个:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let firstLocation = locations.first

您使用的委托(delegate)签名并不完全是委托(delegate)所寻找的。更正签名后,您不再需要 as CLLocation 转换。

我确认它适用于该更改。为了将来引用,您可以在 didUpdateLocations 设置一个断点,您会立即看到它没有被调用,然后从那里向后工作。

还要确保 Info.plist 包含所有必要的位置相关隐私字符串(我通常只放入所有三个以涵盖所有基础)。

关于ios - 位置正常,但无法追踪我在 Swift 3 中行驶的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40634925/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com