TCN
TCN

Reputation: 1651

why isnt this function returning any values?

i need to pass the variables to another function, but it isnt returning anything. NOTE: it doesnt return anything to the outside, but if i do a document.write inside the function it works perfectly..

<script type="text/javascript"> 
//funtion1
    if (navigator.geolocation){
        navigator.geolocation.getCurrentPosition(Location);
        }
    function Location(position){
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
    return (latitude,longitude);
        }


//function2
    function initialize() {
        document.write(latitude);
        document.write(longitude);}
</script>

Upvotes: 1

Views: 391

Answers (5)

kubetz
kubetz

Reputation: 8556

Try to change your code into this:

<script type="text/javascript">
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var latitude = position.coords.latitude;
      var longitude = position.coords.longitude;
      document.write(latitude, longitude);
    });
  }
</script>

The function you pass into getCurrentPosition() will get executed when the position is retrieved. Only then you have access to the position and you are able to set latitude and longitude variables and write them into the document.

In your code getCurrentPosition() will be called, then immediately after that document.write(latitude,longitude); will get executed which is way before the latitude and longitude are available. You must move your document.write() into the Location().

Update 1 (based on comments):

If you don't want to do document.write() inside the callback function, but you want call another function, then you must call it from the callback function.

It will look like this:

<script type="text/javascript">
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var latitude = position.coords.latitude;
      var longitude = position.coords.longitude;
      myFunction(latitude, longitude);
    });
  }

  function myFunction(latitude, longitude) {
    // you can do here anything you wish
    document.write(latitude, longitude);
  }
</script>

Update 2 (based on comments and updated question):

A way how to handle loading the map based on the position is to to call initialize() from Location() with latitude and longitude as parameters. To make sure the code working with HTML elements is executed when page is loaded you can execute inside Location().

window.onload = intialize(latitude, longitude);

See this for a complete code.

Upvotes: 2

pimvdb
pimvdb

Reputation: 154818

You're misunderstanding a few concepts.

First, return (a, b) will evaluate to return b. The comma operator is almost never needed. If you want to return an array, use return [a, b].

Secondly, getCurrentPosition is asynchronous. It will execute Location if it has found the position. So at the time you call document.write, the position has not been fetched yet.

Lastly, you're not using the return value at all. If you want to use the return value of a function, use e.g. var result = func(...). However, in this case this doesn't make sense.

I don't exactly know what you're after, but anything that relies on the position should be put inside the callback (Location in this case). Only when the callback is called the poision is available. In this callback returning something is pointless since the return value is not used for getCurrentPosition.

Edit: Looking at your code, you want something like this instead of returning:

function Location(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;

    useValuesForSomethingElse(latitude, longitude); // call other function with position data
}

Upvotes: 9

nnnnnn
nnnnnn

Reputation: 150010

The .getCurrentPosition() function executes asynchronously and then calls Location() when done. In the meantime, whatever comes after the call to getCurrentPosition() will execute but will have no results to work with. This also means whatever you return from Location() will not be used by any other code.

So you need to call your other function from within Location().

(Also, talking about function returns in general, you need to return a single value or an object/array. A comma-separated list of return values will in effect just return the last one in the list.)

Upvotes: 2

ThiefMaster
ThiefMaster

Reputation: 318478

navigator.geolocation.getCurrentPosition calls the passed function asynchronously as soon as the position is available (e.g. after the user accepted the prompt if the site may know the position).

You need to continue your code in the Location function - and do not return anything as it will not be used anywhere.

Upvotes: 1

Selvakumar Ponnusamy
Selvakumar Ponnusamy

Reputation: 5533

You can return only one value from the function. If you want to return more than one value, create a object and set the values in it to return. Otherwise use arrays to return more than one value

Upvotes: 2

Related Questions