<!DOCTYPE html>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
<input type="button" value="我的位置" style="width:80%; height:100px;" name="position" />
</div>
</div>
<script type="text/javascript" src="cordova-2.5.0.js"></script>
<script type="text/javascript" src="js/jquery-1.7.1.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript">
$(function () {
// 获取位置信息成功时调用的回调函数
// 该方法接受一个“Position”对象,包含当前GPS坐标信息
var onSuccess = function (position) {
alert('纬度: ' + position.coords.latitude + '\n' +
'经度: ' + position.coords.longitude + '\n' +
'海拔: ' + position.coords.altitude + '\n' +
'精确度: ' + position.coords.accuracy + '\n' +
'海拔高度准确性: ' + position.coords.altitudeAccuracy + '\n' +
'方向: ' + position.coords.heading + '\n' +
'速度: ' + position.coords.speed + '\n' +
'时间戳: ' + new Date(position.timestamp) + '\n');
};
// onError回调函数接收一个PositionError对象
function onError(error) {
alert(JSON.stringify(error));
}
navigator.geolocation.getCurrentPosition(onSuccess, onError,{ maximumAge: 3000, timeout: 10000, enableHighAccuracy: true });
$("input[name='position']").live({
click: function () {
/*navigator.notification.alert("提示一个", function(){
alert("忽略了");
}, "提示","确定");*/
}
});
});
</script>
</body>
</html>
这个写法是没问题的,关键是要注意加
{ maximumAge: 3000, timeout: 10000, enableHighAccuracy: true }
maximumAge:当用户获取一次位置过后,在此设置的时间内都不会再更新地理位置