关于phonegap地理位置获取失败的问题

<!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:当用户获取一次位置过后,在此设置的时间内都不会再更新地理位置


重中之重:一定不要在屋子里获取地理位置,大部分会失败的,一定要安装到真机跑到屋子外面去测试,唉,蛋疼的问题,抓狂困扰了好几天
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值