使用JavaScript获取当前位置时获取错误地理编码器失败
我在尝试使用Google Maps API获取当前位置时遇到以下错误 错误:使用JavaScript获取当前位置时获取错误地理编码器失败,javascript,google-maps,Javascript,Google Maps,我在尝试使用Google Maps API获取当前位置时遇到以下错误 错误: Geocoder failed 我在下面提供我的代码 <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> function getDefaultLocation(){ setTimeout(function(){
Geocoder failed
我在下面提供我的代码
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
function getDefaultLocation(){
setTimeout(function(){
var geocoder='';
geocoder = new google.maps.Geocoder();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
codeLatLng(lat, lng)
}
function errorFunction(){
console.log("Geocoder failed");
}
function codeLatLng(lat, lng){
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK){
if (results[1]) {
var indice=0;
for (var j=0; j<results.length; j++){
if (results[j].types[0]=='locality'){
indice=j;
break;
}
for (var i=0; i<results[j].address_components.length; i++){
if (results[j].address_components[i].types[0] == "locality") {
city = results[j].address_components[i];
}
if(results[j].address_components[i].types[0] == "administrative_area_level_1"){
region = results[j].address_components[i];
}
if (results[j].address_components[i].types[0] == "country"){
country = results[j].address_components[i];
}
if(results[j].address_components[i].types[0] == "route"){
locality=results[j].address_components[i];
}
}
}
// console.log('final result',city.long_name,region.long_name,country.long_name,locality.long_name);
var city=city.long_name;
var country=country.long_name;
var locality=locality.long_name;
document.getElementById('bindCon').innerHTML=country; document.getElementById('bindCit').innerHTML=city; document.getElementById('bindloc').innerHTML=locality;
}else{
console.log("No results found");
}
}else{
console.log("Geocoder failed due to: " + status);
}
})
}
},5000);
}
函数getDefaultLocation(){
setTimeout(函数(){
var-geocoder='';
geocoder=新的google.maps.geocoder();
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(successFunction,errorFunction);
}
功能成功功能(位置){
var lat=位置坐标纬度;
var lng=位置坐标经度;
codeLatLng(lat,lng)
}
函数errorFunction(){
console.log(“地理编码器失败”);
}
功能代码LATLNG(lat,lng){
var latlng=新的google.maps.latlng(lat,lng);
geocoder.geocode({'latLng':latLng},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[1]){
var指数=0;
对于(var j=0;jit实际工作,您允许域获取您的位置吗?@Roljhon:它对localhost工作正常,但在chrome中错误为1消息“只允许安全源”
,对于firefox,错误为{code:2,消息:“获取位置时出现未知错误”}
。对于https
它工作正常,但我有http
。啊,我明白了,我认为这是一个限制,localhost被谷歌视为安全的源代码,出于测试目的,我想有一些解决方法,我想,在这里可以看到测试,但对于生产,我认为它要求您的域是安全的,但在Mozila中,它是dif不同的错误。