Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/480.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery,在html5中使用地理位置延迟_Javascript_Jquery_Html_Deferred - Fatal编程技术网

Javascript jquery,在html5中使用地理位置延迟

Javascript jquery,在html5中使用地理位置延迟,javascript,jquery,html,deferred,Javascript,Jquery,Html,Deferred,我刚刚在论坛上得到了我的地理位置经理的一些帮助,因为我陷入了进退两难的境地。 我刚刚意识到地理位置是异步的。我希望它在哪里同步,以便于编程^^ 感谢making3,以下是完成的答案: 我尝试过以某种方式使用$.Deferred(),但失败了。有什么建议吗?:) 在评论中回答了这个问题 GeoManager = function () { this.pos = { lat: 0, lng: 0 }; console.log("Geo ok.

我刚刚在论坛上得到了我的地理位置经理的一些帮助,因为我陷入了进退两难的境地。 我刚刚意识到地理位置是异步的。我希望它在哪里同步,以便于编程^^

感谢making3,以下是完成的答案:

我尝试过以某种方式使用$.Deferred(),但失败了。有什么建议吗?:)

在评论中回答了这个问题

GeoManager = function () {
    this.pos = {
        lat: 0,
        lng: 0
    };
    console.log("Geo ok...");
};

GeoManager.prototype.init = function (callback) {
    var self = this;
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            self.updateLocation(position);
            callback(position);
        });
    } else {
        console.log("Geolocation is not activated!");
    }
};

GeoManager.prototype.updateLocation = function (position) {
    this.pos.lat = position.coords.latitude;
    this.pos.lng = position.coords.longitude;

    console.log(this.pos);
};

GeoManager.prototype.getLat = function () {
    return this.pos.lat;
}
GeoManager.prototype.getLng = function () {
    return this.pos.lng;
};

//returns an object
GeoManager.prototype.getPos = function () {
    return this.pos;
};

var GM = new GeoManager();
GM.init(function () {
    console.log('pos', GM.getPos())
});

那么,你想做什么呢?你想在初始化后阅读
pos
。我想我需要一个回调,我想,我可以在信息设置好后立即使用它。因为它是异步的。我不能用sync来处理它。逻辑是的,设置好后访问this.pos是我的目标:)比如?
GeoManager = function () {
    this.pos = {
        lat: 0,
        lng: 0
    };
    console.log("Geo ok...");
};

GeoManager.prototype.init = function (callback) {
    var self = this;
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            self.updateLocation(position);
            callback(position);
        });
    } else {
        console.log("Geolocation is not activated!");
    }
};

GeoManager.prototype.updateLocation = function (position) {
    this.pos.lat = position.coords.latitude;
    this.pos.lng = position.coords.longitude;

    console.log(this.pos);
};

GeoManager.prototype.getLat = function () {
    return this.pos.lat;
}
GeoManager.prototype.getLng = function () {
    return this.pos.lng;
};

//returns an object
GeoManager.prototype.getPos = function () {
    return this.pos;
};

var GM = new GeoManager();
GM.init(function () {
    console.log('pos', GM.getPos())
});