Jump to content

navigator.geolocation - location unavailable


Drongo_III

Recommended Posts

Hi Guys

 

I've been playing around with navigator.geolocation.getCurrentPosition etc.

 

I've written a very basic test script (below). At the moment I am just playing around to get familiar with it before i try something more sophisticated.

 

But I've hit an issue with it and I'm unsure of how to proceed.  Basically I've tested the script/page below on two mobiles - both running android.

 

On the more modern phone the browser throws up a prompt asking "do you want to give www.XXXXX.com  permission to access to your location?" - hitting the "accept" button then renders out the coords as expected. Great!

 

However, on the other android phone (which is a little older but has google navigator and maps on it so is enabled), I get no prompt for permission and instead an error message is thrown with code 2 - which according to w3c spec. corresponds to:

 

"POSITION_UNAVAILABLE (numeric value 2)

    The position of the device could not be determined. For instance, one or more of the location providers used in the location acquisition process reported an internal error that caused the process to fail entirely. "

 

So I was wondering if anyone had encountered this type of issue and how they fixed it?

 

It seems to me the reason why it's going to code 2 is because the script doesn't have permission to share location data. But i don't know if, or how, you can specifically seek permission.

 

Any help would be much appreciated!

 

Drongo

 

 

 

 

<script src="jquery1.8.js" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function(){

	if(navigator.geolocation){

		navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
	}

else{	
	$('#location').html("Geolocation not supported ");
}

	   
// Success Function to obtain coords	
function successFunction(position) {


		var lat      = position.coords.latitude;
		var longe = position.coords.longitude;
		$('#location').html("This is your lat " +  lat + " and this is your long " + longe);

	}

// Fail function - outputs error code
	function errorFunction(error){

		var error = error.code;
		$('#location').html("The error code is " + error);

	}

});

</script>

Link to comment
Share on other sites

Hmm well after much messing around with this I've found that if I set the accuracy arg to 'true' (default being false) then it finds my coords. The only problem is that using high accuracy initialises the gps on the phone, which takes around 30-40 seconds.

 

Anyway posting this in case it helps someone.

 

navigator.geolocation.getCurrentPosition(successFunction, errorFunction, {
        enableHighAccuracy: true});

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.