javascript - getCurrentPosition成功回调如何传参?

标签 javascript windows-phone-7

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

如何将 devicereadyfoundLoc 传递给 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.');
    }
};

最佳答案

将地理位置回调包装在 function(position) {} 中,如下所示。然后,您可以向实际的回调函数添加任意数量的参数。

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) {},
};

希望这对可能偶然发现此问题的其他人有所帮助。

关于javascript - getCurrentPosition成功回调如何传参?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14740942/

相关文章:

javascript - 为什么我的ajax请求不起作用?

javascript - onclick 事件发生而没有实际发生 click 事件 - js

javascript - Jquery PrettyPhoto 从 URL 中删除#prettyphoto

c# - 如何在WindowsPhone 7 中清除图像缓存。将UriSource 设置为null 不起作用!在 WP7 中清除图像缓存还有哪些其他变体?

Silverlight WP7 工具包,DatePicker\TimePicker 字体大小问题

silverlight - 如何从独立存储中读取图像并将其显示在图像元素上?

javascript - 传单 map 上的固定图像

javascript - 使用 MySQL/Javascript 计算百分比

c# - "System.MissingMethodException was unhandled"?

windows-phone-7 - Windows Phone 中的增强现实