2016-02-29 67 views
0

我正在做一個簡單的速度計來學習Swift和地理定位,並且我堅持最大速度。我想顯示用戶獲得的最大速度,但是我的標籤顯示當前的速度。下面是函數的代碼:保持速度計應用程序的最大速度

func locationManager(manager:CLLocationManager, didUpdateLocations locations:[CLLocation]) { 
     var latestLocation = locations.last as CLLocation! 
     var maxSpeed: Double = 0 

     latLabel.text = "\(latestLocation.coordinate.latitude)" 
     lonLabel.text = "\(latestLocation.coordinate.longitude)" 

     var speed: CLLocationSpeed = CLLocationSpeed() 
     speed = locationManager.location!.speed 
     if(speed<0){ 
      speed=0; 
     } 
     speedLabel.text = String(format: "%.0f km/h", speed * 3.6) 

     maxSpeed = max(maxSpeed, speed) 

     maxSpeedLabel.text = String(format: "%.0f km/h", maxSpeed*3.6) 
    } 

我想的是,speedLabel顯示當前速度,以及maxSpeedLabel改變僅在當前速度較高。如果我不清楚,這裏是什麼是它現在做:

(速度 - 最高速度)

3 - 3

4 - 4

5 - 5

4 - 4

這裏是我想達到的目標:

(速度 - 最高速度)

3 - 3

4 - 4

5 - 5

4 - 5

6 - 6

3 - 6

我不知道爲什麼我的代碼不工作,我錯過了什麼?

感謝您的幫助!

回答

0

你必須初始化的最高速度了「FUNC的LocationManager」,因爲最高速度它inited 0在每個「迭代」的(位置的變化):)所以最大速度將永遠比速度

+0

這是小這麼簡單...非常感謝,我會盡快接受你的回答! (從現在開始〜7分鐘) – nommis

+0

沒問題,祝你有個美好的一天! –

+0

謝謝,你也是! – nommis