2016-11-08 40 views
1

我有以下功能在我Scala.js程序:地理位置斯卡拉JS

def geo():Unit={ 
    var window = document.defaultView 
    val nav = window.navigator 
    val geo: Geolocation = nav.geolocation 
    def onSuccess(p:Position) = { 
    println(s"latitude=${p.coords.latitude}")    // Latitude 
    println(s"longitude=${p.coords.longitude}")    // Longitude 
    } 
    def onError(p:PositionError) = println("Error") 
    geo.watchPosition(onSuccess _, onError _) 
} 

我打電話從我的主要功能該功能。但是經過一段時間之後,它會持續打印經緯度。我只想打印一次。我無法理解我在這裏做錯了什麼,我應該怎麼做才能讓它一次又一次地停止打印?

回答

1

,你觀察到的一個,這樣您可以儘快使用clearWatch駐足觀望的位置:

def geo(): Unit = { 
    val window = document.defaultView 
    val nav = window.navigator 
    val geo: Geolocation = nav.geolocation 
    var watchID: Int = 0 
    def onSuccess(p:Position) = { 
    println(s"latitude=${p.coords.latitude}")    // Latitude 
    println(s"longitude=${p.coords.longitude}")    // Longitude 
    geo.clearWatch(watchID) // only observe one position 
    } 
    def onError(p:PositionError) = println("Error") 
    watchID = geo.watchPosition(onSuccess _, onError _) 
} 
+0

感謝很多:) – Ishan