回调地狱,地理位置-JavaScript
Callback Hell, Geolocations - JavaScript
我在回调地狱,我真的不知道如何摆脱它。我试过阅读其他涉及这个主题的话题,但我仍然没有理解这个概念。
我正在使用谷歌地图API v3,我正在尝试返回用户的当前位置,这样我就可以继续将其传递给其他方法。
我已经成功地断章取义地使用了jQuery.done(),它抛出了一个警告,但我设法得到了我的坐标对象。
如果我调用userPosition,如何让该方法在返回变量userPosition之前等待findUser方法?
编辑:上传了错误的代码,编辑后,缩进很糟糕。:)
userPosition: function(position)
{
if (typeof position === 'undefined')
{
//Call on findUser, when ready callback it self to set variables
}
else
{
//var userPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
//return userPosition;
}
},
findUser: function()
{
var self = this;
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(
function(position) { // If successful
self.userPosition(position);
},
function(e) { // If something went wrong
var errors = {
1: 'Permission denied',
2: 'Position unavailable',
3: 'Request timeout'
};
alert("Error: " + errors[e.code]);
},
{ enableHighAccuracy: true, timeout: this.timeoutVal, maximumAge: 0 }
);
}
else
{
// Set flag
}
},
以下是我在这个jsfiddle中的操作方法。
var thing = {
findUser: function (callback) {
var _this = this;
// use the getPos method to get the geolocation
this.getPos(function (position) {
var lat, lng, userPos;
lat = position.coords.latitude;
lng = position.coords.longitude;
// add the new lat lng to the userPosition variable
_this.userPosition = new google.maps.LatLng(lat, lng);
// invoke the callback
callback();
});
return this;
},
getPos: function (callback) {
var geo = navigator.geolocation;
if (geo) {
// get the geolocation and pass it back to findUser
geo.getCurrentPosition(callback);
}
return this;
}
}
// if userPosition doesn't exist then grab the geolocation
if (!thing.userPosition) {
thing.findUser(function () {
console.log(thing.userPosition);
});
} else {
console.log(thing.userPosition);
}
var isUserLocationSet = false;
userPosition: function(position){
if(!isUserLocationSet)
{
settimeout(userPosition, 200);
return;
}
if (typeof position === 'undefined')
{
var userPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
return userPosition;
}
console.log(typeof position);
},
findUser: function(){
//set isUserLocationSet to true on completion in this method
var self = this;
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(
function(position) { // If successful
self.userPosition(position);
},
function(e) { // If something went wrong
var errors = {
1: 'Permission denied',
2: 'Position unavailable',
3: 'Request timeout'
};
alert("Error: " + errors[e.code]);
},
{ enableHighAccuracy: true, timeout: this.timeoutVal, maximumAge: 0 }
);
}
else
{
// Set flag
}
}
在这里,您可以使用settimeout反复检查该标志,并将继续调用该函数,直到该标志被设置为止。
相关文章:
- 为什么我的javascript地理编码函数返回空白结果
- Javascript 地理位置:将坐标存储在数组中
- javascript逐位并产生奇怪的结果
- parse.com javascript地理查询api中的withinFile错误
- JavaScript逐位屏蔽
- Javascript按位运算符“<<”,“>>>”到Python
- 使用 javascript 获取位图的像素
- Javascript 按位运算符和画布动画
- 我们能否让谷歌地图Javascript地理编码API v3在响应中返回自定义标识符
- 高效安全的Javascript按位/移位操作
- JavaScript:64位对数字的影响
- Javascript逐位运算符混淆
- javascript地理位置不工作,除非我包括警报显示/延迟脚本
- Javascript按位到正则操作符
- javascript地理定位在html5页面中的计算是不同的每个移动浏览器
- Javascript地理定位
- 32位无符号JavaScript按位操作很简单
- JavaScript和位操作符
- Javascript按位操作
- JavaScript中位运算符的克隆函数列表