2016-08-23 3 views
1

デバイスが移動している速度を簡単なラベルで表示したいと考えています。CLLocationSpeedを使用してデバイスが移動している速度を表示する

私はリンゴのウェブサイト上でこれを見つけた:

VAR速度:CLLocationSpeed {取得}

source

上記のコードはm/sでスピードを置くと私はそのためにそれを変換することができます時間あたりのマイル数など

速度を表示するにはどうすればよいですか?

スナップチャットはフィルタとして機能するため、これを行うことができます。

+2

Welcome to StackOverflow!まだ何か試しましたか?通常、特定のライブラリを使用する方法を尋ねるのではなく、特定の質問を念頭に置いてこのWebサイトにアクセスします。 – Orin

+0

クイック返信ありがとう!私は現在の場所を使用してものを試して、インターネットの周りに無限に思われるものを検索したが、速度を表示する上で何かを見つけることができません。 – key9060

答えて

2

は私が@Leo

Swift: Exception while trying to print CLLocationSpeed "unexpectedly found nil while unwrapping an Optional value" によってSO上で、ここでこれを見つけた:

 myLabel = UILabel() 
     myLabel.text  = "MySpeed: \(speed)" 
     self.addChild(myLabel) 

だけで作る:あなたのviewDidLoadで、その後

import UIKit 
import CoreLocation 
class ViewController: UIViewController, CLLocationManagerDelegate { 
let locationManager = CLLocationManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    locationManager.delegate = self 
    if NSString(string:UIDevice.currentDevice().systemVersion).doubleValue > 8 { 
     locationManager.requestAlwaysAuthorization() 
    } 
    locationManager.desiredAccuracy=kCLLocationAccuracyBest 
    } 

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 
    var speed: CLLocationSpeed = CLLocationSpeed() 
    speed = locationManager.location.speed 
    println(speed); 
    } 

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
    if status != CLAuthorizationStatus.Denied{ 
     locationManager.startUpdatingLocation() 
    } 
    } 
} 

、またはどこ、あなたはラベルを作ることができますあなたがそれを使ってみようとしているところでは、「スピード」変数が有効範​​囲に入っていることを確かめてください(私はデモしませんでした)。

私のためにコンパイルされました。お役に立てれば。私はこれまでこれを使用していませんでしたが、検索機能を使用して私はここでほとんどのことを学ぶことができます:D

+0

ありがとう!私はあなたに数分で結果を知らせます! – key9060

+0

ああ待っています。ラベル付きの部品を忘れてしまいました。 – Fluidity

+0

ラベルセクションに部品を追加しましたが、初期化して宣言していませんでした。 'myLabel \t = \t UILabel()'今すぐ – Fluidity

2

これはあなたが探している以上のものかもしれませんが、

import UIKit 
import MapKit 
import CoreLocation 

class ViewController: UIViewController, CLLocationManagerDelegate { 
    //MARK: Global Var's 
    var locationManager: CLLocationManager = CLLocationManager() 
    var switchSpeed = "KPH" 
    var startLocation:CLLocation! 
    var lastLocation: CLLocation! 
    var traveledDistance:Double = 0 
    var arrayMPH: [Double]! = [] 
    var arrayKPH: [Double]! = [] 

    //MARK: IBoutlets 
    @IBOutlet weak var speedDisplay: UILabel! 
    @IBOutlet weak var headingDisplay: UILabel! 
    @IBOutlet weak var latDisplay: UILabel! 
    @IBOutlet weak var lonDisplay: UILabel! 
    @IBOutlet weak var distanceTraveled: UILabel! 
    @IBOutlet weak var minSpeedLabel: UILabel! 
    @IBOutlet weak var maxSpeedLabel: UILabel! 
    @IBOutlet weak var avgSpeedLabel: UILabel! 

    //MARK: Life Cycle 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     minSpeedLabel.text = "0" 
     maxSpeedLabel.text = "0" 
     // 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() 
     } 


    } 


    // 1 mile = 5280 feet 
    // Meter to miles = m * 0.00062137 
    // 1 meter = 3.28084 feet 
    // 1 foot = 0.3048 meters 
    // km = m/1000 
    // m = km * 1000 
    // ft = m/3.28084 
    // 1 mile = 1609 meters 
    //MARK: Location 
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     let location = locations.last 
     if (location?.horizontalAccuracy > 0) { 
      updateLocationInfo(location!.coordinate.latitude, longitude: location!.coordinate.longitude, speed: location!.speed, direction: location!.course) 
     } 
     if lastLocation != nil { 
      traveledDistance += lastLocation.distanceFromLocation(locations.last!) 
      if switchSpeed == "MPH" { 
       if traveledDistance < 1609 { 
        let tdF = traveledDistance/3.28084 
        distanceTraveled.text = (String(format: "%.1f Feet", tdF)) 
       } else if traveledDistance > 1609 { 
        let tdM = traveledDistance * 0.00062137 
        distanceTraveled.text = (String(format: "%.1f Miles", tdM)) 
       } 
      } 
      if switchSpeed == "KPH" { 
       if traveledDistance < 1609 { 
        let tdMeter = traveledDistance 
        distanceTraveled.text = (String(format: "%.0f Meters", tdMeter)) 
       } else if traveledDistance > 1609 { 
        let tdKm = traveledDistance/1000 
        distanceTraveled.text = (String(format: "%.1f Km", tdKm)) 
       } 
      } 
     } 
     lastLocation = locations.last 

    } 

    func updateLocationInfo(latitude: CLLocationDegrees, longitude: CLLocationDegrees, speed: CLLocationSpeed, direction: CLLocationDirection) { 
     let speedToMPH = (speed * 2.23694) 
     let speedToKPH = (speed * 3.6) 
     let val = ((direction/22.5) + 0.5); 
     var arr = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"]; 
     let dir = arr[Int(val % 16)] 
     //lonDisplay.text = coordinateString(latitude, longitude: longitude) 

     lonDisplay.text = (String(format: "%.3f", longitude)) 
     latDisplay.text = (String(format: "%.3f", latitude)) 
     if switchSpeed == "MPH" { 
      // Chekcing if speed is less than zero or a negitave number to display a zero 
      if (speedToMPH > 0) { 
       speedDisplay.text = (String(format: "%.0f mph", speedToMPH)) 
       arrayMPH.append(speedToMPH) 
       let lowSpeed = arrayMPH.minElement() 
       let highSpeed = arrayMPH.maxElement() 
       minSpeedLabel.text = (String(format: "%.0f mph", lowSpeed!)) 
       maxSpeedLabel.text = (String(format: "%.0f mph", highSpeed!)) 
       avgSpeed() 
//    print("Low: \(lowSpeed!) - High: \(highSpeed!)") 
      } else { 
       speedDisplay.text = "0 mph" 
      } 
     } 

     if switchSpeed == "KPH" { 
      // Checking if speed is less than zero 
      if (speedToKPH > 0) { 
       speedDisplay.text = (String(format: "%.0f km/h", speedToKPH)) 
       arrayKPH.append(speedToKPH) 
       let lowSpeed = arrayKPH.minElement() 
       let highSpeed = arrayKPH.maxElement() 
       minSpeedLabel.text = (String(format: "%.0f km/h", lowSpeed!)) 
       maxSpeedLabel.text = (String(format: "%.0f km/h", highSpeed!)) 
       avgSpeed() 
//    print("Low: \(lowSpeed!) - High: \(highSpeed!)") 
      } else { 
       speedDisplay.text = "0 km/h" 
      } 
     } 

     // Shows the N - E - S W 
     headingDisplay.text = "\(dir)" 

    } 

    func avgSpeed(){ 
     if switchSpeed == "MPH" { 
      let votes:[Double] = arrayMPH 
      let votesAvg = votes.reduce(0, combine: +)/Double(votes.count) 
      avgSpeedLabel.text = (String(format: "%.0f", votesAvg)) 
      //print(votesAvg) 
     } else if switchSpeed == "KPH" { 
      let votes:[Double] = arrayKPH 
      let votesAvg = votes.reduce(0, combine: +)/Double(votes.count) 
      avgSpeedLabel.text = (String(format: "%.0f", votesAvg)) 
      //print(votesAvg 
     } 
    } 

    //MARK: Buttons 
    @IBOutlet weak var switchSpeedStatus: UIButton! 
    @IBAction func speedSwtich(sender: UIButton) { 
     if switchSpeed == "MPH" { 
      switchSpeed = "KPH" 
      switchSpeedStatus.setTitle("KPH", forState: .Normal) 
     } else if switchSpeed == "KPH" { 
      switchSpeed = "MPH" 
      switchSpeedStatus.setTitle("MPH", forState: .Normal) 
     } 
    } 

    @IBAction func restTripButton(sender: AnyObject) { 
     arrayMPH = [] 
     arrayKPH = [] 
     traveledDistance = 0 
     minSpeedLabel.text = "0" 
     maxSpeedLabel.text = "0" 
     headingDisplay.text = "None" 
     speedDisplay.text = "0" 
     distanceTraveled.text = "0" 
     avgSpeedLabel.text = "0" 
    } 

    @IBAction func startTrip(sender: AnyObject) { 
     locationManager.startUpdatingLocation() 
    } 


    @IBAction func endTrip(sender: AnyObject) { 
     locationManager.stopUpdatingLocation() 
    } 




} 
関連する問題