我尝试使用带有 phonegap 的 jquery mobile 开发应用程序。在第一页输入寄存器号,然后使用 watchposition 获取 gps 值,然后将其位置更新到 sqlite 数据库。因为我尝试使用 clearwatch() 方法停止观察位置。我有以下错误。Uncaught TypeError: Object # has no method 'clearwatch' my index.html
<!Doctype html>
<html>
<head>
<title>Geo position Aware Vehicle Locator</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"/>
<script src="js/jquery-1.9.0.min.js"></script>
<link rel="stylesheet" href="css/jquery.mobile-1.3.0-rc.1.min.css"/>
<script src="js/jquery.mobile-1.3.0-rc.1.min.js"></script>
<script src="js/register.js"></script>
<script src="js/terminate.js"></script>
<script src="js/update.js"></script>
<script>
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
navigator.notification.alert("PhoneGap is ready!");
}
$(document).ready(function () {
$("#regis").click(register);
$("#start").click(update);
});
</script>
</head>
<body>
<div data-role="page" data-theme="b">
<div data-role="header" data-theme="b">
<p align="center">Vehicle Registration</a>
</div>
<div data-role="content" data-theme="b">
<label>Give Route Number</label>
<input type="text" id="rno"> <a href="#second" id="regis" data-transition="pop" data-role="button"
data-inline="true" data-icon="arrow-r" data-iconpos="right" data-mini="true"
data-theme="b">Register</a>
</div>
</div>
<div id="second" data-role="page" data-theme="b">
<div data-role="header" data-theme="b">
<p align="center">Vehicle Status</p>
</div>
<div data-role="content" data-theme="b">
<div class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;">
<h1>Current Location:</h1>
<h3> Latitude:</h3>
<p id="lat"></p>
<h3>Longitude:</h3>
<p id="long"></p>
<h3>Current Time:</h3>
<p id="utime"></p> <a id="start" data-role="button" data-theme="b">start</a>
<a id="terminate" data-role="button" data-icon="delete" data-iconpos="right" data-theme="b">Terminate</a>
</div>
</div>
</div>
</body>
</html>
和我的 terminate.js
document.addEventListener("deviceready", function () {
console.log("devicere");
if (navigator.network.connection.type == Connection.NONE) {
console.log("check con");
$("#terminate").text('No Internet Access')
.attr("data-icon", "delete")
.button('refresh');
}
});
var rno = localStorage.getItem('rno');//get value from local storage
console.log("rno=>", rno);
var watch_id = ''; // ID of the geolocation
var tracking_data = []; // Array containing GPS position objects
$("#start").on('click', function () {
watch_id = navigator.geolocation.watchPosition(
// Success
function (position) {
console.log(position);
var lat = position.coords.latitude;
console.log("lat", lat);
var lon = position.coords.longitude;
console.log("lon", lon);
var utime = position.timestamp;
console.log("position", utime);
$("#lat").html(lat);
$("#long").html(lon);
$('#utime').html(utime);
console.log('position' + lat + lon + utime);
$.ajax({
type: "POST",
url: "/update",
data: 'vid=' + rno + '&lat=' + lat + '&lon=' + lon + '&utime=' + utime,
cache: false
});
},
// Error
function (error) {
console.log(error);
},
// Settings
{ frequency: 3000, enableHighAccuracy: true });
});
//stop tracking gps value
$("#terminate").on('click', function () {
navigator.geolocation.clearwatch(watch_id);
var url = "/terminate";
$(location).attr('href', url);
});