地理编码器仅适用于chrome,不适用于Firefox / IE / android

Geocoder only working with chrome, not with firefox/IE/android

本文关键字:适用于 Firefox IE android 不适用 chrome 编码器      更新时间:2023-09-26

>我正在尝试制作登录页面,使用地理编码器将访问者重定向到正确的语言以获取国家/地区代码,然后重定向到正确的语言。如果我在芬兰,它会将我重定向到 www.mydomain.com/fi/index.html 从 www.mydomain.com/index.html

这些重定向仍在进行中。当地理编码在所有浏览器上工作时,我将修复这些问题!

这是我的代码

<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>My test site</title> 
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
  var 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(){
    alert("Geocoder failed");
}
function initialize() {
    geocoder = new google.maps.Geocoder();

}
  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) {
      console.log(results)
        if (results[1]) {
             for (var i=0; i<results[0].address_components.length; i++) {
            for (var b=0;b<results[0].address_components[i].types.length;b++) {
                if (results[0].address_components[i].types[b] == "country") {
                    city= results[0].address_components[i];
                    break;
                }
            }
        }
        var countrycode = city.short_name.toLowerCase();
        if (countrycode == "gb") {
            countrycode = "en";
        }
        if (countrycode == "se" || countrycode == "ru") {
            window.location.replace("/" +countrycode+ "/index.html");
        }
        else if (countrycode == "fi") {
            window.location.replace("/fi/index.html");
        }
        else {
            window.location.replace("/en/index.html");
        }
        } else {
          alert("No results found");
        }
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  }
</script> 
</head> 
<body onload="initialize()">
</body> 
</html>

此代码在谷歌浏览器中运行良好,但在Firefox,IE或Android中没有任何作用。其他浏览器要求允许使用位置,但随后没有任何反应。

你能帮我在其他浏览器上完成这项工作吗?

谢谢!

以下代码仅适用于 Firefox。它不适用于Chrome和IE。也许您可以再次更改此代码以使其在Chrome和Firefox上运行。我不知道这是否可以在IE上工作。我将对此进行研究,稍后更新我的代码。

<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>My test site</title> 
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
function successFunction(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    codeLatLng(lat, lng);
}
function errorFunction(){
    alert("Geocoder failed");
}
var geocoder;
function initialize() {
  geocoder = new google.maps.Geocoder();

  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
  } 
}
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]) {
         for (var i=0; i<results[0].address_components.length; i++) {
            for (var b=0;b<results[0].address_components[i].types.length;b++) {
                if (results[0].address_components[i].types[b] == "country") {
                    city= results[0].address_components[i];
                    break;
                }
             }
         }
      }
      var countrycode = city.short_name.toLowerCase();
      alert(countrycode);
    }
  });
}
</script> 
</head> 
<body onload="initialize()">
</body> 
</html>