【问题标题】:Geolocation in scala jsscala js中的地理位置
【发布时间】:2016-11-08 18:24:32
【问题描述】:

我的 Scala.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 _ )
}

我从我的主函数调用这个函数。但它在一段时间后连续打印经度和纬度。我只想打印一次。我无法理解我在这里做错了什么,我应该怎么做才能让它一次又一次地停止打印?

【问题讨论】:

    标签: scala.js


    【解决方案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 _)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多