我正试图通过Geolocation
API获取用户的位置,直到昨天工作正常,但今天我得到了#34;无法获得位置"每次尝试
用于API的脚本是
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript">
var geocoder;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
//Get the latitude and the longitude;
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
//var num = parseFloat(lat);
//var str = num.toFixed(10);
//str = str.substring(0, str.length-7);
$('#yourlat').val(lat);$('#yourlat1').val(lat);
//var num1 = parseFloat(lng);
//var str1 = num1.toFixed(10);
//str1 = str1.substring(0, str1.length-7);
$('#yourlong').val(lng);$('#yourlong1').val(lng);
codeLatLng(lat, lng)
}
function errorFunction(){
alert("Geocoder failed");
}
function initialize() {
geocoder = new google.maps.Geocoder();
}
function codeLatLng(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
var addressget ='';
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results)
if (results[1]) {
//formatted address
//alert(results[0].formatted_address)
addressget = results[0].formatted_address;
$('#yourcity').html(addressget);
$('#yourcitys').val(addressget);
//find country name
for (var i=0; i<results[0].address_components.length; i++) {
for (var b=0;b<results[0].address_components[i].types.length;b++) {
//there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
//this is the object you are looking for
city= results[0].address_components[i];
break;
}
}
}
//city data
var stateget ='';
stateget = city.short_name + " " + city.long_name;
$('#yourstate').html(stateget);
}
else {
alert("We are not able to get your location..");
}
}
else {
alert("Geocoder failed due to: " + status);
}
});
}
</script>
我在bodyload上调用函数
<body onload="initialize()">
每次我允许获取位置时,它会给出错误We are not able to get your location..