INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

GeoLocation Variable problem

GeoLocation Variable problem

(OP)
Hi there

I have a fairly simple problem I'm hoping someone can help me with. I have the following script

CODE --> Javascript

<script>
var my_lat;
var my_lng;

function geo_success(position)
	{
    my_lat = position.coords.latitude;
    my_lng = position.coords.longitude;
	}

function geo_error()
	{
    alert("Sorry, no position available.");
	}

var geo_options =
	{
    enableHighAccuracy: true, 
    maximumAge        : 30000, 
    timeout           : 27000
	};

navigator.geolocation.watchPosition(geo_success, geo_error, geo_options);

document.write(my_lat);
</script> 

I am trying to set a global variable called my_lat and my_lng and have the geolocation function set these variables so that I can call them later on. For now I'm just writing to set that this is working properly but I'm just getting an undefined error.

What am I doing wrong here? Thanks!

NATE

RE: GeoLocation Variable problem

Hi

Quote (NATE)

I'm just getting an undefined error
First of all, you could give more detailed explanation, but for now I assume that you mean, the global variables remain undefined as only the registered error callback function is called.

Which browser are you using ? In FireFox the geolocation to work, the about:config configuration has to be set to true. Does that apply to you ?

Which browser's which version are you using ? And are you accessing your page through https://, file:// or http://localhost ?

Quote (Firefox 55 for developers)

Security
( Firefox 55 for developers )

What is the reason of failure ? Your error callback function gets informed, you just need to handle the information :

CODE --> JavaScript(fragment)

function geo_error(err)
{
    alert("Sorry, no position available :\n - code : " + err.code + "\n - message : " + err.message);
} 

And anyway, your logic is flawed. You set up callback functions then expect the next line of code to already have results. Accessing a device's GPS may take some time. But even if there is no GPS available and location is guessed based on IP and/or provider, there is still extremely little chance the callback to be performed in meantime. You should display the coordinates from geo_success() function. But definitely not with document.write().

Feherke.
feherke.github.io

RE: GeoLocation Variable problem

The main issue is that geolocation takes some time to complete. You are trying to retrieve your global variables through the document.write function before they've had time to get set.

Try this after calling :

CODE

navigator.geolocation.watchPosition(geo_success, geo_error, geo_options);
alert(my_lat);
setTimeout(function(){
    alert(my_lat);
}, 2000); 

The first alert will show undefined, the one in the timeOut method should show a coordinate.

Note that setTimeOut stops all execution, so the browser may seem like its frozen.


Extra tip, unless absolutely necessary, its best to avoid document.write. There are other, better ways to write text to the browser window.

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech

RE: GeoLocation Variable problem

(OP)
Thanks I was only using document.write to see if the variable was passing or not, I won't actually be using it at all. Wanted to know the variables were setting properly before using while calling another function.

NATE

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close