开发者

Is there a way to detect whether or not mobile device has location services enabled with JavaScript?

开发者 https://www.devze.com 2023-04-10 00:15 出处:网络
I am currently using Modernizr to detect whether or not geolocation is supported for a particular device. However, I want to display a more specific error message if geolocation is supported, but the

I am currently using Modernizr to detect whether or not geolocation is supported for a particular device. However, I want to display a more specific error message if geolocation is supported, but the user has disabled location services for their device.

I'm getting complaints that geolocatio开发者_高级运维n isn't working on supported browsers, when in fact it's just that the user hasn't enabled theirs.

I know there are ways to detect this using native mobile code, but is there a way to do this with JavaScript? Does Modernizr support this?


Does your error callback ever fire? If you don't have one, try adding one. If it never fires, you can set a timeout on your function that if it does not receive permission within a certain amount of time, to display a notification.

Since the errorCallback never fires if geo is disabled, create a wrapper around the function that will create a settimeout on another function. If either of the callbacks to the api do fire, then remove the settimeout before it fires.

var timeOutId;
function disabledGeoHandler(){...}
function show_map() {
    clearTimeout(timOutId);
    ...
}
function show_map_error() {
    clearTimeout(timOutId);
    ...
}
function lookup_location() {
  timeOutId = setTimeout(disabledGeoHandler, 1000);
  navigator.geolocation.getCurrentPosition(show_map, show_map_error);
}

In fact, you could just use geo.js

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号