Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 geolocation not working

I'm trying to learn how to use HTML5 geolocation and am having trouble getting the following script to run properly:

<script src="js/jquery-1.4.2.min.js"></script>
<script>
    jQuery(window).ready(function(){
        jQuery("#btnInit").click(initiate_geolocation);
    });

    function initiate_geolocation() {
        navigator.geolocation.getCurrentPosition(handle_geolocation_query);
    }

    function handle_geolocation_query(position){
        alert('Lat: ' + position.coords.latitude + ' ' +
              'Lon: ' + position.coords.longitude);
    }
</script>

My HTML contains a button that references the #btnInit jQuery function; however, the script does not display any alert popup boxes. Moreover, it seems that the script is pausing prior to the line: jQuery(window).ready(function(){

What are my next steps for getting HTML5 geolocation working?

like image 245
Vee Avatar asked Dec 23 '25 03:12

Vee


1 Answers

Your code works perfectly for me, see this fiddle. Is your browser supporting geolocation?

Depending on your browser, you should see somewhere a notice to allow the browser to get your location. You must allow this, to get your code working.

like image 172
topek Avatar answered Dec 24 '25 19:12

topek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!