- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用最新版本的 Xcode,目前正在尝试为最新版本的 IOS 构建应用程序。该应用程序是一个事件跟踪程序,需要速度、距离、时间和位置。位置和时间工作正常,但速度和距离代码不起作用。 UILabels 不会改变。
let manager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let reigon:MKCoordinateRegion = MKCoordinateRegion(center: myLocation, span: span)
map.setRegion(reigon, animated: true)
self.map.showsUserLocation = true
self.map.userTrackingMode = .followWithHeading
//Speed and distance
let mps = location.speed
let km = mps * 3.6
if watch.isRunning {
print(String(format: "%.2f", km))
speedText.text = String(format: "%.2f", km)
if startLocation == nil {
startLocation = locations.first
} else if let location = locations.last {
traveledDistance += lastLocation.distance(from: location)
print("Traveled Distance:", traveledDistance)
let dist = traveledDistance * 3.6
speedText.text = String(format: "%.2f", dist)
print("Straight Distance:", startLocation.distance(from: locations.last!))
}
lastLocation = locations.last
} else {
distanceText.text = String("00.00")
speedText.text = String("00.00")
return
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
if (error as? CLError)?.code == .denied {
manager.stopUpdatingLocation()
manager.stopMonitoringSignificantLocationChanges()
}
}
这是我的完整代码...
import UIKit
import MapKit
import CoreLocation
class ExcersiseViewController: UIViewController {
var timerText: UILabel!
var speedText: UILabel!
var distanceText: UILabel!
var action: UIButton!
var map: MKMapView!
let watch = Stopwatch()
let saveSystem = SaveCompleted()
var startLocation: CLLocation!
var lastLocation: CLLocation!
var traveledDistance: Double = 0
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self as? CLLocationManagerDelegate
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
manager.distanceFilter = 10
let lightGreyColor = UIColor(red: 68/255, green: 68/255, blue: 68/255, alpha: 1)
view.backgroundColor = .black
let bounds = UIScreen.main.bounds
let width = bounds.size.width
let height = bounds.size.height
map = MKMapView(frame: CGRect(x: 0, y: height - (height / 2), width: width, height: height - (height / 2)))
map.showsUserLocation = true
map.userTrackingMode = .followWithHeading
action = UIButton(frame: CGRect(x: 0, y: 20, width: width, height: 50))
action.backgroundColor = lightGreyColor
action.setTitleColor(.white, for: .normal)
action.addTarget(self, action: #selector(startStop), for: .touchUpInside)
if watch.isRunning {
action.setTitle("Stop", for: .normal)
} else {
action.setTitle("Start", for: .normal)
}
timerText = UILabel(frame: CGRect(x: 0, y: height / 7, width: width, height: 50))
timerText.text = "00:00:00"
timerText.textAlignment = .center
timerText.textColor = .white
timerText.font = UIFont(name: "Avenir Next", size: 60)
speedText = UILabel(frame: CGRect(x: 0, y: height / 3, width: width, height: 50))
speedText.text = "00.00"
speedText.textAlignment = .left
speedText.textColor = .white
speedText.font = UIFont(name: "Avenir Next", size: 60)
distanceText = UILabel(frame: CGRect(x: 0, y: height / 3, width: width, height: 50))
distanceText.text = "00.00"
distanceText.textAlignment = .right
distanceText.textColor = .white
distanceText.font = UIFont(name: "Avenir Next", size: 60)
self.view.addSubview(distanceText)
self.view.addSubview(speedText)
self.view.addSubview(timerText)
self.view.addSubview(action)
self.view.addSubview(map)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
let manager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let reigon:MKCoordinateRegion = MKCoordinateRegion(center: myLocation, span: span)
map.setRegion(reigon, animated: true)
self.map.showsUserLocation = true
self.map.userTrackingMode = .followWithHeading
//Speed and distance
let mps = location.speed
let km = mps * 3.6
if watch.isRunning {
print(String(format: "%.2f", km))
speedText.text = String(format: "%.2f", km)
if startLocation == nil {
startLocation = locations.first
} else if let location = locations.last {
traveledDistance += lastLocation.distance(from: location)
print("Traveled Distance:", traveledDistance)
let dist = traveledDistance * 3.6
speedText.text = String(format: "%.2f", dist)
print("Straight Distance:", startLocation.distance(from: locations.last!))
}
lastLocation = locations.last
} else {
distanceText.text = String("00.00")
speedText.text = String("00.00")
return
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
if (error as? CLError)?.code == .denied {
manager.stopUpdatingLocation()
manager.stopMonitoringSignificantLocationChanges()
}
}
@objc func updateElapsedTimeLabel(timer : Timer) {
if watch.isRunning {
let hours = Int(watch.elapsedTime/60/60)
let minutes = Int(watch.elapsedTime.truncatingRemainder(dividingBy: 60)/60)
let seconds = Int(watch.elapsedTime.truncatingRemainder(dividingBy: 60))
timerText.text = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
} else {
timer.invalidate()
}
}
@objc func startStop() {
if watch.isRunning {
watch.stop()
action.setTitle("Start", for: .normal)
//Save code
let time = timerText.text
let distance = distanceText.text
if time != "" && distance != "" {
saveSystem.runFromView(info: "Activity data: Time taken: \(time), Distance travelled: \(distance)")
timerText.text = "00:00:00"
distanceText.text = "00.00"
speedText.text = "00.00"
self.dismiss(animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Alert", message: "There was an error saving the activity. We are sorry for the inconvinience. Please try again or contact support if the error persists.", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
return
}
} else {
watch.start()
Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ExcersiseViewController.updateElapsedTimeLabel(timer:)), userInfo: nil, repeats: true)
action.setTitle("Stop", for: .normal)
}
}
}
这里还有我的秒表脚本...注意:这部分可以工作,速度代码也可以在 Xcode 9 上使用。
import Foundation
class Stopwatch {
private var startTime : NSDate?
var elapsedTime: TimeInterval {
if let startTime = self.startTime {
return -startTime.timeIntervalSinceNow
} else {
return 0
}
}
var isRunning: Bool {
return startTime != nil
}
func start() {
startTime = NSDate()
}
func stop() {
startTime = nil
}
}
有人对这个问题有想法吗?我添加了 Mapkit 和 CoreLocation 包并设置了正确的使用说明。注意:我已经以编程方式创建了整个 View ,因此它没有链接到 Storyboard。
最佳答案
您必须将CLLocationManagerDelegate
设置为您的 View Controller
所以替换
manager.delegate = self as? CLLocationManagerDelegate
与
manager.delegate = self
并在此处添加CLLocationManagerDelegate
class ExcersiseViewController: UIViewController,CLLocationManagerDelegate {
....
}
关于ios - 如何在 Xcode 10 中使用 CLLocation 获取速度和距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55016415/
这个问题在这里已经有了答案: How to convert CLLocation to string in Xcode (3 个答案) 关闭 6 年前。 我有一个包含 String 格式的位置的变量
我正在尝试将位置检测添加到应用程序中,并且我已经弄明白了大部分内容。但是我在使用 reverseGeocodeLocation 方法时遇到了问题。我收到错误 cannot convert value
我尝试在 map View 上显示用户当前位置,但在位置管理器功能的第一行出现错误 这是我的代码 import UIKit import MapKit class FirstViewControlle
我的 CLLocation 遇到问题。 我想知道两个坐标之间的距离,以便将其显示在 tableView 中位置标题旁边: static NSString *CellIdentifier = @
如何在后台保持 CLLocation 更新。我相信您需要注册应用程序才能在应用程序委托(delegate)中执行此操作,但我在任何地方都找不到对此的引用? 最佳答案 以下是相关文档的链接: https
我在CLLocation distanceFromLocation上遇到了一些问题。我下面的代码经过格式化,可以帮助调试,显式分配两个新位置,等等。无论哪种方式,两个几乎相同的位置都会产生巨大的差距,
据我所知,最大值和最小值是: 纬度: [-90, 90] 经度: [-180, 180] CLLocation 仍然接受超出这些限制的值,例如: [[CLLocation alloc] initWi
我是 Swift 的新手(以及这个网站,如果我做错了什么,很抱歉),我正在尝试制作一个正在运行的应用程序来跟踪用户的位置。虽然我用来跟踪距离的函数有效,但它并不是从 0 开始。当我点击开始按钮时,距离
在 iPhone 6+、IOS 11.4、Xcode 9.4.1 上,CLLocation 时间戳始终为零。 纬度:30.598748经度:-97.820877海拔高度:308.921658H准确度:
我试图在内存中保存 self.mapView.userLocation.location在CLLocation属性,但在我将值分配给属性并检查属性的值后,我得到了这个: +/- 0.00m (spe
我已经设法使用 GLGeocoder geocodeAddressString 获取了 2 个城市的坐标 从名称中检索到这 2 个城市的坐标后,我想计算它们之间的距离。但是我面临一个问题,因为 CLL
我正在尝试设置一个自定义类的 CLLocation 属性(名为 objectLocation),使用下面的代码,从我的主 ViewController 调用。不幸的是,我在“注释”行收到一条错误消息,
更新到 iOS 8 后我遇到了很多麻烦。以下代码在我更新到 iOS 和 Xcode 6 之前有效。代码是在 viewDidLoad 中编写的: self.locationManager.delegat
我的类 ViewController 中有这段代码: CLLocationManager *locationManager; - (void)viewDidLoad { [super view
我的代码中有一个奇怪的问题。我想调用 [locationManager startUpdatingLocation]; 并且当更新完成时 - (void)locationManager:(CLLoca
所以我不知道发生了什么。我的代码运行得非常好,然后我在 Storyboard中工作了一会儿,突然我开始收到错误消息,即我的 CLLocation 在解包时始终为 nil。我做了我能想到的或在网上阅读的
info.plist 文件: NSLocationWhenInUseUsageDescription This project would like to know your location NSL
我想在应用程序启动之间保留 2 个 CLLocation。这是我希望在两次启动之间存储在内存中的唯一数据。我该怎么做呢? CLLocation 是否有 writetofile 或将其存储在 cored
这是我的位置管理器委托(delegate)代码。 当我们使用汽车移动时,它不会给出速度,所以至少速度值应该改变。 它总是给出常量值 -1.00。 -(void)locationManager:(CLL
我想使用可靠的位置,即使是在旧的 iPhone 上。然而,许多读数(尤其是来自手机信号塔的读数)都太不准确了。我想。 当我绘制我的位置 + 精度半径(或查看谷歌地图应用程序)时,我注意到估计圆的中心通
我是一名优秀的程序员,十分优秀!