地理编码器仅适用于chrome,不适用于Firefox / IE / android
Geocoder only working with chrome, not with firefox/IE/android
>我正在尝试制作登录页面,使用地理编码器将访问者重定向到正确的语言以获取国家/地区代码,然后重定向到正确的语言。如果我在芬兰,它会将我重定向到 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>
相关文章:
- html5 drawImage适用于firefox,而不是chrome
- JS适用于Firefox和Safari,但不适用于Chrome.此处'是我的网站
- 通过单击主菜单外部关闭子菜单'不适用于IE,但适用于Firefox&铬
- jQuery放大版不适用于Chrome和Safari,但适用于Firefox
- JavaScript调整iFrame大小-适用于Firefox/IE,但不适用于Chrome
- jquery脚本仅适用于firefox
- CSS/JQuery/Javascript加载图标仅适用于firefox
- jquery 2.2 serializeArray()适用于Firefox、Chrome,但不适用于IE(11)
- Ajax,Php-Postback仅适用于Firefox
- jQuery scrollTop()-仅适用于Firefox,不适用于Chrome
- AngularJS下拉菜单适用于Firefox,不适用于Chrome和Safari
- 在 javascript 中更改图像 .style 仅适用于 Firefox
- JS幻灯片适用于Firefox和chrome,但不适用于Internet Explorer
- jquery datatable oTable.$ 适用于 Firefox,但不适用于 Chrome
- 为什么这适用于Firefox,但不适用于Chrome或IE
- Javascript 适用于 Firefox 和 Chrome,但不适用于 IE
- Javascript适用于Firefox而不是IE.为什么
- 删除cookie(PHP,JS)适用于Firefox,Safari,Opera,但不适用于Chrome
- JavaScript脚本适用于Firefox,Chrome,Safari,Internet Explorer< 9,但不
- Web Audio onaudioprocess适用于Firefox,JSFiddle适用于Chrome,但不适用于Ch