HTML5 geolocation won't work in Firefox, Chrome and Chromium

☆樱花仙子☆ 提交于 2020-01-14 17:53:47

问题


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 ?


回答1:


Have you read this ? http://code.google.com/p/chromium/issues/detail?id=41001

At the end of the thread they come to the conclusion that in order to work in Chrome, geolocation must be performed on a device with a working wifi adapter.

Was wifi enabled on your computer ?

(dunno for firefox)




回答2:


I have to wait until the document is loaded to get it work in chrome

jQuery().ready(function() {
   if (navigator.geolocation) { 
        navigator.geolocation.getCurrentPosition....
    }
});



回答3:


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");
};


来源:https://stackoverflow.com/questions/11841803/html5-geolocation-wont-work-in-firefox-chrome-and-chromium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!