Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the user's location from within a Chrome extension?

I've created an extension and I'm trying to get the user's location so I can display it to them.

I've tried adding this code:

var x = document.getElementById("ua");
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        x.innerHTML = "Unavailable.";
    }
}
function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude; 
}

But nothing comes up in the extension, any advice? I'm also trying to avoid using the Chrome location API as it is only available on the Dev Channel.

like image 813
BillyMays Avatar asked Jan 20 '16 23:01

BillyMays


1 Answers

The Declare Permissions page in the documentation lists a special permission for your case:

"geolocation"
Allows the extension or app to use the proposed HTML5 geolocation API without prompting the user for permission.

You must use it, because the permission infobar cannot be shown for some extension pages (background, popup).

like image 146
Xan Avatar answered Oct 25 '22 00:10

Xan