I have spent last 3 days until I got gps working on android using phonegap to develope application.
I am using example to understand how it works but I have some problem when I try to send position to a data base.
This is my code:
<!DOCTYPE html>
<html>
<head>
<title>Device Properties Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-1.8.1.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
navigator.geolocation.getCurrentPosition(onSuccess, onError, {
enableHighAccuracy : true,
maximumAge : Infinity,
timeout : 15000
});
}
// onSuccess Geolocation
//
function onSuccess(position) {
var element = document.getElementById('geolocation');
var id = 1;
pagina = window.open( "http://mywebpage:80/open/posicionCamion.php?lat=" + position.coords.latitude + "&lon=" + position.coords.longitude + "&id=" + id);
pagina.close();
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'Altitude: ' + position.coords.altitude + '<br />' +
'Accuracy: ' + position.coords.accuracy + '<br />' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' +
'Heading: ' + position.coords.heading + '<br />' +
'Speed: ' + position.coords.speed + '<br />' +
'Timestamp: ' + position.timestamp + '<br />';
}
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
</script>
</head>
<body>
<p id="geolocation">Finding geolocation...</p>
</body>
</html>
If I delete pagina.window and pagina.close lines, it works fine. But I want to send position to a php file each 15 seconds and this php file will update the DB each time.
With these lines, application closes but nothing is sended. There is a way to do what I want?
I tested php file using a webbrowser and it works fine.
Thanks.
window.open()opens a browser window, which will not work as expected in devices. Try replacing thewindow.open()call with an ajax request.