I'm using Modernizr to help detect different things on a browser. Now, I know most recent browsers support geolocation (caniuse chart here), but what if it is turned off by the user preferences? In that case, Modernizr returns true with the geolocation
class on the html
tag since it does have support.
I've been using geolocation support to make a "get directions" type of map. Here's the gist of it:
What works
- The map works fine with directions to the desired destination;
- In browsers with no support at all, a map highlighting the desired destination's address (say a restaurant) is shown; This has been kept in as a fallback to prevent having an empty space on the page.
Where I'am stuck
I've been looking around and can't seem to find any documentation on how to deal with a browser (mobile or not) which does support geolocation but has it disabled by user preferences.
What I've been working with
CSS
I'm working off of this here post which shows a working solution where the user is shown a map with directions to the proper destination if geolocation is supported. In the case where geolocation is not supported, I have an alternate content div ready to show.
/* geo location is supported */
html.geolocation #regular-map {
display: block;
}
html.geolocation #alternate-map {
display: none;
}
/* geo location is NOT supported, show alternate map */
html.no-geolocation #regular-map {
display: none;
}
html.no-geolocation #alternate-map {
display: block;
}
What I've tried
Permission denied error code
I've tried using this type of detection to see if the user has blocked geolocation (based on this post), but when I test it on a browser where I did block it, I get the notification of "supported":
if(navigator.geolocation)
{
alert("supported");
}
else
{
function errorCallback(error)
{
if (error.code = error.PERMISSION_DENIED)
{
alert("blocked");
}
else
{
alert("not supported");
}
}
}
Bottom Line
Bottom line, what I need is to see if the browser supports geolocation, but the user has chosen to Deny using geolocation services.
Edit
The general idea is like this. I'm more comfortable with PHP, so here's the idea:
<?php
if($hasgeosupport)
{
?>
<script src="hassupport.js"></script>
<div id="geolocation"></div>
<?php
}
else
if($hasgeosupport)
{
?>
<script src="fallback.js"></script>
<div id="fallback"></div>
<?php
}
?>
Edit 2
Here's where I'm at, this morning. I have the code creating the proper HTML structure. However, the map is not loading.
if (navigator.geolocation)
{
$(document).ready(function() {
// Does not create HTML without the above line
$.getScript('js/maps.js'); // includes required scripts and variables needed
var regular_map = '<div data-role="content"><div class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;"><div id="map_canvas" style="height:350px;"></div></div><div id="results" style="display:none;"><div id="directions"></div></div></div>';
$("#basic-map").prepend(regular_map);
}
}
else
{
// fallback here
}
I don't know why this is not working for me. I have the proper scripts, the HTML is generated.
Same goes for the fallback. My HTML is generated, yet the maps never show.