Phonegap Geolocation API 总是得到错误代码 3
Posted
技术标签:
【中文标题】Phonegap Geolocation API 总是得到错误代码 3【英文标题】:Phonegap Geolocation API always getting error code 3 【发布时间】:2014-04-15 06:50:11 【问题描述】:我在我的phonegap android应用程序中使用phonegap geolocation API。它总是显示错误代码3,超时错误。我也尝试将超时变量值更改为增加,使HighAccuracy为真。但结果没有变化。但是它可以在浏览器上运行,而不是在应用程序中运行。给我这个问题的解决方案。
这是我的代码
<!DOCTYPE html>
<html>
<head>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />
<link rel="stylesheet" href="css/contact.css" type='text/css' />
<link rel="stylesheet" href="css/jquery.mobile-1.3.1.min.css"
type='text/css' />
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCJAbwOQjaVjDy9XJ0UEZCvTdZeW_atXak&sensor=false">
</script>
<script type="text/javascript" charset="utf-8" src="cordova-2.9.0.js"></script>
<script src="jquery.mobile/jquery-1.10.2.min" type='text/javascript'></script>
<script>
document.addEventListener("deviceready", onDeviceReady, false);
$(document).on("mobileinit", function()
$.mobile.phonegapNavigationEnabled = true;
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
$.mobile.loadingMessageTextVisible = true;
$.mobile.pageLoadErrorMessage;
);
$(document).ready(function()
$('#Login').show();
$('#Contacts').hide();
);
function success(name)
alert(name);
function error()
alert("error");
function login()
var name = $("#name").val();
if (navigator.notification)
navigator.notification.alert("Welcome " + name, alertDismissed, 'Sample App', 'Ok');
else
alert("Welcome " + name);
var options =
enableHighAccuracy: true,
timeout: 8000,
maximumAge: 10000
;
navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
$("#Login").hide();
$("#Contacts").show();
function alertDismissed()
console.log("ok");
function onSuccess(position)
var latt1 = position.coords.latitude;
var lont1 = position.coords.longitude; //alert(latt1+" "+lont1);
var myLatlng1 = new google.maps.LatLng(latt1, lont1);
var mapOptions1 =
center: myLatlng1,
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
;
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions1);
var marker1 = new google.maps.Marker(
position: myLatlng1,
map: map,
title: "You are here! (at least within a " + position.coords.accuracy + " meter radius)"
);
var circle = new google.maps.Circle(
radius: 26000,
strokeColor: "#00FF00",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#C0C0C0",
fillOpacity: 0.35,
map: map
);
marker1.setIcon("./images/blue_dot_circle.png");
marker1.setMap(map);
function onError(error)
if (navigator.notification)
navigator.notification.alert('code: ' + error.code + '\n' + 'message: ' + error.message + '\n', alertDismissed, 'Sample App', 'Ok');
else
alert('code: ' + error.code + '\n' + 'message: ' + error.message + '\n');
function AlertConfirmed(buttonIndex)
if (buttonIndex == 1)
device.exitApp();
console.log(buttonIndex);
else
console.log(buttonIndex);
function goHome()
navigator.app.loadUrl("http://www.google.co.in",
openExternal: true
);
</script>
<script src="jquery.mobile/jquery.mobile-1.3.1.min.js"
type='text/javascript'></script>
</head>
<body>
<div data-role="page" >
<div id="Login">
<div data-role="header" data-position="fixed" data-theme="b"
data-tap-toggle="false" class="header" data-transition="none"
id="myFixedHeader">
<p>Sample Application</p>
</div>
<div data-role="content">
<div id="title">
<div
style="width: 90%; padding-left: 5%; padding-right: 5%; padding-top: 3%; padding-bottom: 1%;">
<input type="text" name="password" id="name" data-mini="true"
placeholder="Enter Your Name " value="" data-inline="true">
</div>
<center>
<div id="log" onclick="login()">Login</div>
</center>
</div>
</div>
<div data-role="footer" data-theme="b" data-position="fixed"
class="footer" data-transition="none" data-tap-toggle="false">
<p onclick="goHome()">© 2014
Sample App Reserved, LLC.</p>>
</div>
</div>
<div id="Contacts">
<div data-role="header" data-theme="b" data-tap-toggle="false"
data-position="fixed" class="header" data-transition="none">
<h3 id="htitle">Home Page</h3>
</div>
<div data-role="content" id="map_canvas" >
</div>
<div data-role="footer" data-theme="b" data-position="fixed"
class="footer" data-transition="none" data-tap-toggle="false">
<p onclick="goHome()">© 2014
Sample App Reserved, LLC.</p>
</div>
</div>
</body>
</html>
I have added Permissions INTERNET,FINE_LOCATION,COARSE_LOCATION in manifest file
and corrova 2.9.0.js
【问题讨论】:
在哪个版本的android 确保您在清单中添加了权限。 在浏览器中会显示地理位置,但在 APP 中不会显示我已经测试了 2.1 到 4.2 等所有版本我添加了 ACCESS_FINE_LOCATION、ACCESS_COARSE_LOCATION、Internet 的权限 【参考方案1】:请确保您在 Android 清单中设置了 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
。
【讨论】:
【参考方案2】:I made one mistake in config.xml.Now,I have added
<access origin="*"/>
<access origin="*://*.googleapis.com/*" subdomains="true" />
<access origin="*://*.gstatic.com/*" subdomains="true" />
<access origin="*://*.google.com/*" subdomains="true" />
<access origin="*://*.googleusercontent.com/*" subdomains="true" />
in my config.xml file.googlemap geolocation showed successfully
【讨论】:
【参考方案3】:在 app/res/xml/config.xml 中创建条目
条目:
在 app/AndroidManifest.xml 中
条目:
还要确保 enablehighAccuracy 参数设置为 true。
干杯!!
【讨论】:
以上是关于Phonegap Geolocation API 总是得到错误代码 3的主要内容,如果未能解决你的问题,请参考以下文章
Android Geolocation onError Phonegap
使用 Geolocation 和 Google Maps API [帮助]
phonegap android中navigator.geolocation.getCurrentLocation的奇怪行为
PhoneGap/Cordova 将 GeoLocation 从 localStorage vars 推送到 MySQL
在 Android 2.0+ 上的 WebView 中使用 navigator.geolocation.getCurrentPosition(PhoneGap 相关)