0

I'm attempting to assign a global variable to HTML5 geolocation coordinates in JavaScript and passing these via jQuery to a form field.

The problem I'm having is assigning the lat and long to a global variable. I know the geolocation api is exposed via "navigator.geolocation" but I must be missing something in assigning these values to the global variable.

Here's my attempt:

    var latitude = null;
function lat()
{
    if (navigator.geolocation)
    {
        latitude = position.coords.latitude;
    }
}

var longitude = null;
function lon()
{
    if (navigator.geolocation)
    {
        longitude = position.coords.latitude;
    }
}

Any help in ironing out the mistake would be greatly appreciated.

Edit: Tried this. Doesn't work in obtaining the value but also doesn't result in a Firebug error:

    var latitude = navigator.geolocation.getCurrentPosition(function(position){
  lat = position.coords.latitude
  return lat
});

var longitude = navigator.geolocation.getCurrentPosition(function(position){
  lon = position.coords.longitude
return lon});
1

2 Answers 2

1

..edit2: updating with more useful example for callbacks..

function requestCurrentPosition(){
        if (navigator.geolocation)
        {
        navigator.geolocation.getCurrentPosition(useGeoData);
        }
}

 function useGeoData(position){
    var longitude = position.coords.longitude;
    var latitude = position.coords.latitude;
    /*do stuff with long and lat here.*/
  }

..edit: updating example...

var latitude = null;
function lat()
{
if (navigator.geolocation)
{
    navigator.geolocation.getCurrentPosition(
        function(position){
              latitude = position.coords.latitude;
        });
}
}

var longitude = null;
function lon()
{
if (navigator.geolocation)
{
    navigator.geolocation.getCurrentPosition(
        function(position){  
            longitude = position.coords.latitude;
        });
    }
}
}

You might want to check out this HTML5 Demo

Sign up to request clarification or add additional context in comments.

10 Comments

The map function works fine. Just need to be able to access the lat and long at a global level and am having a challenge assigning a global variable those values.
This is close. I changed var latitude = null to var latitude = new lat() and the same for longitude. What appeared in the form, though, was [object Object]. It is getting closer, but not quite. Using "null," nothing appears in the form.
Its a call back function, so your cant just assign it like that. Add a console.log(latitude) and console.log(longitude) inside the function(position) blocks, the value will be defined, however if you assign latitude =lat() your actually assigning the variable to the function pointer
getCurrentPosition takes in a callback function and doesnt return anything because the user may have to give permission for the site to use the geolocation information. If you want to work with the position data, you will need to do it within the function(position) call
A little confused. So how do I get the actual numeric value assigned to the variable? Because "null" returns nothing to the form. JS is a bit confusing. Your insight is very appreciated.
|
0

How about something more like this:

// Does this browser support geolocation?
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(locationSuccess, locationError);
}
else{
    showError("Your browser doesn't support geolocation!");
}

// Now get user's location

function locationSuccess(position) {
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.