我使用钛工作室(3.4.1),用JavaScript为iOS和安卓编写。
我试图获取设备的纬度和经度,并将其返回给一个函数,以便在另一个模块中调用该函数并将其传递给另一个函数。
这是geo.js,在里面我正在加载Ti.Geolocation.getCurrentPosition并试图将延迟返回到getLat()函数。然后,通过导出将其提供给app.js。
exports.getLat = function(){
Ti.Geolocation.getCurrentPosition(function(e) {
console.log(e);
timeout : 10000;
return JSON.stringify(e.coords.latitude);
});
};这是app.js,它检查应用程序运行的是哪个平台。它看到了一个iPhone,然后需要geo.getLat()。在此之后,我希望将纬度存储到变量lat中,稍后使用它将其作为参数提供给另一个函数,如getWdata(lat);
if (Ti.Platform.osname === 'android') {
console.log('android version\n');
var geo = require('geo');
var lat = geo.getLat(0);
var lng = geo.getLng(0);
console.log('Android Coordinates: ' + lat, lng);
}
else if (Ti.Platform.osname === 'iphone' || 'ipad'){
console.log('iOS version\n');
var geo = require('geo');
var lat = geo.getLat();
geo.getLat();
console.log('iOS Coordinates: ' + lat);
}发布于 2015-02-23 06:50:22
您将需要使用回调来完成此任务。简单的过程是将一个函数(称为回调)作为参数传递,然后使用return;调用回调函数。
另外,您不需要调用两种获取纬度/经度的方法,因为只能使用一种方法。
例子如下:
In geo.js:
exports.getLatLong = function(callback){
Ti.Geolocation.getCurrentPosition(function(e) {
if (e.success) {
Ti.API.info("Cords latitude" + e.coords.latitude);
Ti.API.info("Cords longitude" + e.coords.longitude);
callback(e.coords.latitude, e.coords.longitude);
} else {
alert("Unable to fetch cords");
}
});
};现在在app.js__中,将getLatLong函数调用为:
var geo = require('geo'), lat = 0, long = 0;
geo.getLatLong(function(latitude,longitude) {
lat = latitude;
long = longitude;
});注意:使用Ti.API.info而不是console.log__。
发布于 2015-02-23 04:41:39
使用:
Titanium.Geolocation.getCurrentPosition(function(e) {
if (e.success) {
Ti.API.info("Cords longitude" + e.coords.longitude);
Ti.API.info("Cords latitude" + e.coords.latitude);
}
});https://stackoverflow.com/questions/28662876
复制相似问题