Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GeoLocation Variable problem

Status
Not open for further replies.

SPYDERIX

Technical User
Jan 11, 2002
1,899
0
0
CA
Hi there

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

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
 
Hi

NATE said:
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 [tt]undefined[/tt] as only the registered error callback function is called.

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

Which browser's which version are you using ? And are you accessing your page through file:// or ?
Firefox 55 for developers said:
Security
[ul]
[li]The [tt]Geolocation[/tt] API is now available only to secure contexts (bug 1072859).[/li]
[/ul]
( Firefox 55 for developers )

What is the reason of failure ? Your error callback function gets informed, you just need to handle the information :
Code:
[b]function[/b] [COLOR=orange]geo_error[/color][teal]([/teal]err[teal])[/teal]
[teal]{[/teal]
    [COLOR=orange]alert[/color][teal]([/teal][i][green]"Sorry, no position available :[/green][/i][lime]\n[/lime][i][green] - code : "[/green][/i] [teal]+[/teal] err[teal].[/teal]code [teal]+[/teal] [i][green]"[/green][/i][lime]\n[/lime][i][green] - message : "[/green][/i] [teal]+[/teal] err[teal].[/teal]message[teal]);[/teal]
[teal]}[/teal]

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 [tt]document.write()[/tt].


Feherke.
feherke.github.io
 
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top