我正在使用 JQM 和 Phonegap。我有一个计算两点之间距离的函数。我在 Xcode 中这样做并给了模拟器 lat = 51.4863 long = -3.179169
distance = function(lat1, lon1, lat2, lon2, unit)
{
var radlat1 = Math.PI * lat1/180;
var radlat2 = Math.PI * lat2/180;
var radlon1 = Math.PI * lon1/180;
var radlon2 = Math.PI * lon2/180;
var theta = lon1-lon2;
var radtheta = Math.PI * theta/180;
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist);
dist = dist * 180/Math.PI;
dist = dist * 60 * 1.1515;
dist = dist * 1.609344;
if (unit=="M") { dist = dist / 1.609344 ;}
if (unit=="N") { dist = dist * 0.8684 ;}
alert(dist);
return dist;
}
我还有一个我想返回距离的位置数据库。
如果从我的 db.transactions executeSQL 的 onsuccess 回调中调用 distance 函数,它将返回错误的数字。
但是,当我在 getCurrentPosition 的成功回调上调用此函数时,它工作正常。请记住,我也在此处运行 SQL 选择查询。
我的函数何时工作的代码如下:
$( document ).delegate("#mfinder", "pageinit", function() {
var db = window.openDatabase("Database", "1.0", "location Data", 200000);
db.transaction(inputData, errorCB, inputDataSuccess);
});
function inputData(tx) {
tx.executeSql('DROP TABLE IF EXISTS locations');
tx.executeSql('CREATE TABLE locations (id UNIQUE, name, lat, lon, pcode)');
tx.executeSql('INSERT INTO locations VALUES (1, "My house", 51.4863, -3.179169, "E20QP")');
tx.executeSql('INSERT INTO locations VALUES (2, "Manchester", 51.548577,0.708275, "ABC123")');
}
function inputDataSuccess() {
var db = window.openDatabase("Database", "1.0", "location Data", 200000);
db.transaction(readLocations, errorCB);
}
function readLocations(tx) {
tx.executeSql('select * from locations', [], getDistance, errorCB);
}
function getDistance (tx, r){
d1 = r.rows.item(1).lat;
d2 = r.rows.item(1).lon;
d3 = r.rows.item(1).name;
}
$("#fm").click(function() {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
});
function onSuccess(position) {
clat = position.coords.latitude;
clon = position.coords.longitude;
var x = distance(clat, clon, d1, d2, "M");
alert(x); //THIS WORKS!
}
当函数返回错误的数字时,我的代码是这样的:
clat = 0;
clon = 0;
$("#fm").click(function() {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
var db = window.openDatabase("Database", "1.0", "location Data", 200000);
db.transaction(inputData, errorCB, inputDataSuccess);
});
function onSuccess(position) {
clat = position.coords.latitude;
clon = position.coords.longitude;
}
function inputData(tx) {
tx.executeSql('DROP TABLE IF EXISTS locations');
tx.executeSql('CREATE TABLE locations (id UNIQUE, name, lat, lon, pcode)');
tx.executeSql('INSERT INTO locations VALUES (1, "My house", 51.4863, -3.179169, "E20QP")');
tx.executeSql('INSERT INTO locations VALUES (2, "Manchester", 51.548577,0.708275, "ABC123")');
}
function inputDataSuccess() {
var db = window.openDatabase("Database", "1.0", "location Data", 200000);
db.transaction(readLocations, errorCB);
}
function readLocations(tx) {
tx.executeSql('select * from locations', [], getDistance, errorCB);
}
function getDistance (tx, r){
d1 = r.rows.item(1).lat;
d2 = r.rows.item(1).lon;
d3 = r.rows.item(1).name;
var milesAway = distance(clat, clon, d1, d2, "M"); //wrong figures!!!
alert(milesAway);
}
它工作的第一种方式是这样的......在PageInit中,我正在创建数据并将数据选择到内存中。然后“点击”功能将启用位置服务并执行距离功能。
第二种情况在 pageinit 上没有任何事情发生,一切都在 Click 事件上。在这里,我首先检索用户位置,然后运行数据库并返回一个数据行以计算距离。
现在,有人可以告诉我为什么我得到错误的数字???理想情况下,我希望第二个代码库能够工作,这样,我可以循环加载大量数据以附加到例如 DIV 元素。非常感谢任何查看我的问题的人。