【问题标题】:Phonegap build not sending geolocationPhonegap 构建不发送地理位置
【发布时间】:2017-02-08 20:15:40
【问题描述】:

我正在尝试使用 Phonegap Build 将我的位置发送到服务器。由于某种原因,它似乎不起作用。这可能是什么原因以及如何解决?

这是我的 js

// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);

var watchID = null;

// PhoneGap is ready
//
function onDeviceReady() {
    // Update every 3 seconds
    var options = {maximumAge:10000, enableHighAccuracy:true};
    watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
}

// onSuccess Geolocation
//

 var coords = {lat: "", lon: ""};
function onSuccess(position) {
    coords.lat = position.coords.latitude;
    coords.lon = position.coords.longitude;
}

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}


setInterval ( "Updateposition()", 15000 );
function Updateposition(){
// here you can reuse the object to send to a server
console.log("lat: " + coords.lat);
console.log("lon: " + coords.lon);
var auto = localStorage.getItem("number");

jQuery.ajax({
        type: "POST", 
        url:  serviceURL+"location.php", 
        data: 'x='+ coords.lon+'&y='+coords.lat+'&auto='+auto,
        cache: false
    });

}

在 config.xml 我有

`<gap:plugin name="cordova-plugin-geolocation" source="npm" />`

【问题讨论】:

    标签: cordova geolocation phonegap-build


    【解决方案1】:

    我认为 navigator.geolocation.watchPosition(onSuccess, onError, options);不应在您的 onDeviceReady 函数中。每次位置更改时都会调用 watchPosition。

    您尝试获取 onDeviceReady 的位置,然后将其发送到服务器并使用 navigator.geolocation.watchPosition(onSuccess, onError, options);每次位置变化时调用你的服务器

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多