如何将参数传递给getCurrentPosition成功回调? [英] How to pass parameters to getCurrentPosition success call back?

查看:455
本文介绍了如何将参数传递给getCurrentPosition成功回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在调用 navigator.geolocation.getcurrentPosition 时将一个或多个参数传递给成功回调?

How can I pass in one or more parameters to a success call back when calling navigator.geolocation.getcurrentPosition?

如何将 deviceready foundLoc 传递到 getGeoLoc 方法?

var app = {

    onDeviceReady: function () {
        alert = window.alert || navigator.notification.alert;

        app.getGeoLoc('deviceready');
    },

    getGeoLoc: function (id) {
        navigator.geolocation.getCurrentPosition(this.foundLoc, this.noLoc, { timeout: 3 });
    },

    foundLoc: function (position) {
        var parentElement = document.getElementById('deviceready'); 
        var lat = parentElement.querySelector('#lat');
        var long = parentElement.querySelector('#long');

        lat.innerHTML = position.coords.latitude;
        long.innerHTML = position.coords.longitude;
    },

    noLoc: function () {
        alert('device has no GPS or access is denied.');
    }
};


推荐答案

将地理位置回调包裹在函数中(位置) ){} 如下。然后你可以根据需要为你的实际回调函数添加任意数量的参数。

Wrap the geolocation callback in function(position) {} as follows. Then you can add as many arguments as you want to your actual callback function.

var app = {

    getGeoLoc : function (id) {

        var self = this;

        navigator.geolocation.getCurrentPosition(function(position) {

            var myVar1, myVar2, myVar3; // Define has many variables as you want here

            // From here you can pass the position, as well as any other arguments 
            // you might need. 
            self.foundLoc(position, self, myVar1, myVar2, myVar3)

        }, this.noloc, { timeout : 3});
    },

    foundLoc : function(position, self, myVar1, myVar2, myVar3) {},
};

希望这可以帮助其他任何可能偶然发现此事的人。

Hope this helps anyone else who might stumble upon this.

这篇关于如何将参数传递给getCurrentPosition成功回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆