我正在编写一个iOS应用,其中应用必须在每次加载页面时确定当前的纬度经度。我需要从事件处理程序访问经度和纬度,并将最新的坐标传递给第三方服务器。
我已经能够使用Cordova的navigator.geolocation.getCurrentPosition
检索坐标,但需要帮助:
所以问题是,有人可以告诉我正确的语法,以便能够捕获下面的getlocation脚本中生成的值,这样我就可以将co ords附加到url所以coords =生成的long / lat值< / p>
这是我的pageshow事件处理程序代码:
$("#HomePage").live("pageshow",
function() {
$.mobile.showPageLoadingMsg();
$.get("http://[externalurl]/process.cfc?method=HomePage&CORDS=[LATEST COORDS VALUE]]&returnformat=json", {}, function(res) {
$.mobile.hidePageLoadingMsg();
var s = "";
for(var i=0; i<res.length; i++) {
s+= "<li><a href='employeedetails.html?Id=" + res[i].Id + "'>Employee Details</a></li>";
s+= "<li><a href='OrgDetails.html?OrgId=" + res[i].OrgId + "'>OrgDetails</a></li>";
}
$("#HomePageContent").html(s);
$("#HomePageContent").listview("refresh");
},"json");
});
下面是我用来检索用户当前坐标的代码。我只需要弄清楚如何将值放到上面的jQuery函数中,这样就可以将它们附加到GET请求中。
var x = document.getElementById(“demo”);
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = + position.coords.latitude +
"," + position.coords.longitude;
}
答案 0 :(得分:2)
有一些问题需要纠正:
首先,$.live()
方法很久以前就已弃用,并且在1.9版(http://api.jquery.com/live/)中已从jQuery中完全删除。
其次,jQuery Mobile pageshow
事件也已弃用,将随jQuery Mobile版本1.6(https://api.jquerymobile.com/pageshow/)的发布而删除。
用于捕获页面显示时间的事件处理程序应如下所示:
$(document).on("pagecontainershow", function( event, ui ) {
var elmtId = $('body').pagecontainer('getActivePage').prop('id');
if (elmtId == "HomePage") {
// YOUR CODE HERE
}
});
没有什么可以阻止您直接在上面的getCurrentPosition
事件处理程序中执行pagecontainershow
方法。
可能就是这么简单:
$(document).on("pagecontainershow", function( event, ui ) {
var elmtId = $('body').pagecontainer('getActivePage').prop('id');
if (elmtId == "HomePage") {
navigator.geolocation.getCurrentPosition(function(position) {
var element = document.getElementById('geolocation');
element.innerHTML = position.coords.latitude
+ ','
+ position.coords.longitude;
}, function(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
});
}
});
您需要确保#HomePage
确实引用了jQuery Mobile页面和 not 以及页面内的元素。 jQuery Mobile页面在其最外层元素(http://demos.jquerymobile.com/1.4.5/pages/)上具有data-role="page"
属性。