我试图通过googles geo location api显示用户的位置 它适用于2g连接但不能使用3g或4g连接。 既不成功函数也不称错误函数
<!DOCTYPE html>
<html>
<head>
<style>
input{width:100%;
font-weight:bold;
font-size:18px;
padding:6% 0;
}
</style>
<meta 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>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function codeLatLng(){
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.CELL] = 'Cell generic connection';
states[Connection.NONE] = 1;
if(states[networkState]!=1)
{
var x=document.getElementById("myHeader");
x.innerHTML="Fetching your location";
navigator.geolocation.getCurrentPosition(onSuccess);
}
else{
// alert("please check ur internet connection");
var x=document.getElementById("myHeader");
x.innerHTML="No internet connection";
}
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
function onSuccess(position)
{
var geocoder = new google.maps.Geocoder();
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var latlng = new google.maps.LatLng(lat,lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
var x=document.getElementById("myHeader");
x.innerHTML=results[1].formatted_address;
});
}
}
</script>
</head>
<body>
<div id="panel">
<input type="button" value="Show my location" onclick="codeLatLng()">
<h1 id="myHeader">your location</h1>
</div>
<script type="text/javascript" src="cordova.js"></script>
</body>
</html>
enter code here
答案 0 :(得分:1)
我只是通过添加
来实现它{ enableHighAccuracy: true }
谢谢大家:)