Emulator无法进入showPosition函数-Android PhoneGap
所有功能都在工作,但无法进入Emulator无法进入showPosition函数-Android PhoneGap,android,html,cordova,Android,Html,Cordova,所有功能都在工作,但无法进入showPosition功能 这是我正在使用的代码: <!DOCTYPE html> <html> <body> <p id="demo"> Click the button to get your position: </p> <button onclick="getLocation()">Try It</button> <di
showPosition
功能
这是我正在使用的代码:
<!DOCTYPE html>
<html>
<body>
<p id="demo">
Click the button to get your position:
</p>
<button onclick="getLocation()">Try It</button>
<div id="mapholder">
</div>
<script>
var x = document.getElementById("demo");
function getLocation() {
alert("hellow navigator");
if (navigator.geolocation) {
alert("inside navigator.loaction");
//alert(showPosition);
var tout = 10 * 1000 * 1000;
options = {
enableHighAccuracy: false,
timeout: Infinity,
maximumAge: 0
};
navigator.geolocation.watchPosition(showPosition, showError, options);
} else {
alert("inside else navigator.location");
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
alert("inside Show Position");
var latlon = position.coords.latitude + "," + position.coords.longitude;
var img_url = "http://maps.googleapis.com/maps/api/staticmap?center=" + latlon + "&zoom=14&size=400x300&sensor=false";
document.getElementById("mapholder").innerHTML = "<img src='" + img_url + "'>";
}
function showError(error) {
alert("inside error");
alert(error.code);
switch (error.code) {
case error.PERMISSION_DENIED:
alert("User denied the request for Geolocation.")
break;
case error.POSITION_UNAVAILABLE:
alert("Location information is unavailable.")
break;
case error.TIMEOUT:
alert("The request to get user location timed out.")
break;
case error.UNKNOWN_ERROR:
alert("An unknown error occurred.")
break;
}
}
</script>
</body>
</html>
单击按钮获取您的位置:
试试看
var x=document.getElementById(“演示”);
函数getLocation(){
警报(“hellow navigator”);
if(导航器.地理位置){
警报(“inside navigator.loaction”);
//警报(显示位置);
var tout=10*1000*1000;
选项={
EnableHighAccurance:错误,
超时:无限,
最大值:0
};
导航器、地理位置、观察位置(显示位置、淋浴ROR、选项);
}否则{
警报(“inside else navigator.location”);
x、 innerHTML=“此浏览器不支持地理位置。”;
}
}
功能显示位置(位置){
警报(“内部显示位置”);
var latlon=位置.coords.latitude+“,”+位置.coords.latitude;
var img_url=”http://maps.googleapis.com/maps/api/staticmap?center=“+latlon+”&zoom=14&size=400x300&sensor=false”;
document.getElementById(“地图持有者”).innerHTML=“”;
}
功能错误(错误){
警报(“内部错误”);
警报(错误代码);
开关(错误代码){
案例错误。权限被拒绝:
警报(“用户拒绝了地理定位请求。”)
打破
案例错误。位置不可用:
警报(“位置信息不可用”)
打破
大小写错误。超时:
警报(“获取用户位置的请求超时”)
打破
案例错误。未知错误:
警报(“发生未知错误”)
打破
}
}
我如何允许共享我的位置信息?为什么我调用没有参数的
showPosition
函数,即使它接受我的参数(position)?尝试使用一个数字作为超时,而不是无限
。如果你不想超时,那就不要把它包括在你的选项中,但最好有一个超时。。我使用了有限的时间,比如3秒,它总是将我指向淋浴ROR函数,将我带到超时错误!您正在使用PhoneGap提供的地理位置插件吗?所有必需的权限和插件都已完成,,,我仍然无法调用我的showPosition功能:(您是否尝试过删除它?众所周知,Android上的地理位置插件会导致问题,并且正在被弃用。如果您删除该插件,它将退回具有相同语法的HTML5地理位置API。