HTML5地理位置在Firefox,Chrome和Chromium中无法使用 [英] HTML5 geolocation won't work in Firefox, Chrome and Chromium

查看:602
本文介绍了HTML5地理位置在Firefox,Chrome和Chromium中无法使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用HTML5地理定位API;但我有问题,使其在Firefox浏览器和铬:

pre $ b $初始化();

函数init(){;
//获取当前位置
getPosition();


函数getPosition(){
navigator.geolocation.getCurrentPosition(成功,失败,{
enableHighAccuracy:true,
timeout:10000,
maximumAge:Infinity
});


函数成功(位置){
alert(Your latitude:+ position.coords.latitude +longitude:
+ position.coords。经度);


函数失败(e){
alert(Your position can not be found+ e.code +=>+ e.message);



$ b

在IE9和Safari中,它的工作原理是完美无瑕的,但是:在Firefox(v13和V14)中有一个错误代码3(超时)
$ b


    > Chrome和Chromium(v20和v21)中存在错误代码2,并显示网络位置提供程序信息:https://maps.googleapis.com/maps/api/browserlocation/json?browser=googlechrome&sensor=true:响应格式不正确。


我已经全新安装了Chrome(现在安装在Windows XP上,没有扩展名)地理定位在浏览器中。

你可以试试看:
http://jsfiddle.net/mhj82/38/



是否有解决方案可以在支持地理定位的所有浏览器上使用? / p>

解决方案

if(navigator.geolocation){
var latitude = null;
var longitude = null;
navigator.geolocation.getCurrentPosition(function(position){
latitude = position.coords.latitude;
longitude = position.coords.longitude;
});


$ b}其他{
alert(浏览器不支持地理定位API);
};


I'm trying to use the HTML5 geolocation API; but I have problems to make it work on Firefox Chrome and Chromium :


init();

function init() {;
    // Get the current location
    getPosition();      
}

function getPosition() {
    navigator.geolocation.getCurrentPosition(success, fail,{
        enableHighAccuracy:true,
        timeout:10000,
        maximumAge:Infinity
    });    
}   

function success(position) {
    alert("Your latitude: " + position.coords.latitude + "longitude: "
        + position.coords.longitude);
}

function fail(e) {
    alert("Your position cannot be found"+e.code+" => "+e.message);
}

In IE9 and Safari it works flawlessly; but :

  • in Firefox (v13 and V14) there is an error code 3 (timeout)
  • in Chrome and Chromium (v20 and v21) there is and error code 2 with the message "Network location provider at 'https://maps.googleapis.com/maps/api/browserlocation/json?browser=googlechrome&sensor=true' : Response was malformed."

I have a fresh install of Chrome (installed today on windows XP, no extensions) and I have authorized the geolocation in the browser.

You can try it there : http://jsfiddle.net/mhj82/38/

Is there a solution to make it work on all browser supporting geolocation ?

解决方案

Try this tested in chrome desktop and mobile

if (navigator.geolocation) {
    var latitude = null;
    var longitude = null;
    navigator.geolocation.getCurrentPosition(function (position) {
        latitude = position.coords.latitude;
        longitude = position.coords.longitude;
   });



} else {
    alert("Geolocation API is not supported in your browser");
};

这篇关于HTML5地理位置在Firefox,Chrome和Chromium中无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆