JavaScript怎么处理共享位置数据
本文小编为大家详细介绍"JavaScript怎么处理共享位置数据",内容详细,步骤清晰,细节处理妥当,希望这篇"JavaScript怎么处理共享位置数据"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
可能存在用户不想与您共享其位置数据的情况。为了处理这种情况,您可以在调用函数时提供两个getCurrentLocation()函数。
如果您的地理位置尝试成功,则调用第一个函数,如果您的地理位置尝试失败,则调用第二个函数。让我们看一个例子:
例试试这个代码»
// Set up global variable
var result;
function showPosition() {
// Store the element where the page displays the result
result = document.getElementById("result");
// If geolocation is available, try to get the visitor's position
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
result[xss_clean] = "Getting the position information...";
} else {
alert("Sorry, your browser does not support HTML5 geolocation.");
}
};
// Define callback function for successful attempt
function successCallback(position) {
result[xss_clean] = "Your current position is (" + "Latitude: " + position.coords.latitude + ", " + "Longitude: " + position.coords.longitude + ")";
}
// Define callback function for failed attempt
function errorCallback(error) {
if(error.code == 1) {
result[xss_clean] = "You've decided not to share your position, but it's OK. We won't ask you again.";
} else if(error.code == 2) {
result[xss_clean] = "The network is down or the positioning service can't be reached.";
} else if(error.code == 3) {
result[xss_clean] = "The attempt timed out before it could get the location data.";
} else {
result[xss_clean] = "Geolocation failed due to unknown error.";
}
}
读到这里,这篇"JavaScript怎么处理共享位置数据"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。