簡體   English   中英

Bixby javascript - 如何獲取用戶當前位置

[英]Bixby javascript - How do I get user current location

如何在javascript操作中獲取用戶當前位置? 當我嘗試使用$user.currentLocation我收到一個錯誤

在Action文件中,包含以下代碼以獲取CurrentLocation對象,該對象是您必須導入的viv.geo庫封裝的一部分。 有關如何導入膠囊庫的說明,請參閱此文章https://stackoverflow.com/a/53747472/10409637

computed-input (myLocation) {
  min (Required) max(One)
  type (geo.CurrentLocation)

  compute {
    if ($user.currentLocation.$exists) {
      intent {
        goal: geo.CurrentLocation
        value-set: geo.CurrentLocation { $expr ($user.currentLocation) }
      } 
    }
  }
}

在您的javascript函數中,您可以訪問CurrentLocation對象作為console.log(myLocation) ,結果將是

latitude:21.334342
longitude:-88.89106
$id:null
$type:viv.geo.CurrentLocation

您可以使用此功能

此代碼段不適用於stackOverflow,然后轉到Codepen

試穿: https//codepen.io/lucassoomago/pen/YBzOoW

 var x = document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { x.innerHTML = "Geolocation is not supported by this browser."; } } function showPosition(position) { x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude; } 
 <!DOCTYPE html> <html> <body> <p>Click the button to get your coordinates.</p> <button onclick="getLocation()">Try It</button> <p id="demo"></p> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM