1

如何在 javascript 操作中获取用户当前位置?当我尝试使用时$user.currentLocation出现错误

4

2 回答 2

1

在您的 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
于 2019-01-22T19:12:16.853 回答
-1

你可以使用这个功能

此代码段不适用于 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>

于 2019-01-22T13:29:53.473 回答