We help IT Professionals succeed at work.

Get Latitude/Longitude from within JavaScript causing rror

High Priority
136 Views
Last Modified: 2019-05-28
I have created the following two functions inside my JS file (which will be used in PhoneGap/Cordova) and I have put them outside of the main JavaScript class:

**
 *@description Returns the latitude of the current location
 *@return {float} a latitude
 */
function getLatitude() {
  return position.coords.latitude;
}

/**
 *@description Returns the longitude of the current location
 @return {float} a longitude
 */
function getLongitude() {
  return position.coords.longitude;
}

Open in new window


Why is it, that if I put this into my HTML page, I can get the coordinates - but when I call these functions from within JavaScript, I get an error saying that position is undefined?

What is a workaround so that I only get a latitude or longitude?
Comment
Watch Question

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
I think you've a script somewhere setting the position variable so your javascript need to run after it.
try this :

<script>
jQuery(function($) { // wait for document be loaded
     alert( getLongitude() );
});
</script>

Open in new window

or this one :
<script>
jQuery(window).load(function() { // wait for document and all resources like image, script to be loaded
     alert( getLongitude() );
});
</script>

Open in new window

or :
<script>
jQuery(window).load(function() {
     setTimeout(function() {
           alert( getLongitude() );
     }, 500); // wait 500ms after complete page load
});
</script>

Open in new window


Of course, the best would be to identify that script setting the position variable to know when it's available
CERTIFIED EXPERT

Commented:
Hi Massimo,
Your code is working when you put that code into your HTML page. Means all the pre-requisites are already present. (here I mean position variable).

Now if you put same code into JS file (outside main class), then it is not working. Means pre-Requisites are not exist.

Here you may need to check sequence where you are including JS file into your HTML page.
I mean all pre-Requisites must be referenced first.
Massimo ScolaSoftware Engineer

Author

Commented:
Hi

Here is my new code.


var location = {};

function getLocation(position) {
  location.latitude = position.coords.latitude;
  location.latitude = position.coords.longitude;
}


/**
 *@description Returns the latitude of the current location
 *@return {float} a latitude
 */
function getLatitude() {
  return location.latitude;
}

/**
 *@description Returns the longitude of the current location
 @return {float} a longitude
 */
function getLongitude() {
  if (navigator.geolocation) {
    return location.longitude;
  }
}

function getCoordinates() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(getlocation);
  }

}

Open in new window


When the user presses on the "click order" button, I would like to retrieve the current coordinates.

  $("#placeNewOrder").on('click', function (e) {
      getCoordinates();
      app.megamax.placeNewOrder($("#clients").val());
      goToAddItems();
    });

Open in new window


The problem is that I get an error
Cannot GET /[object%20Object]

If I remove var location = {}; I get no errors.

What is the best way to get the latitude and longitude?

Do I have to change var location = {} to something else?
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
I think you first have a typo line 29; it should be getLocation and not getlocation
try this :
var location = {};

function getLocation(position) {
    location.latitude = position.coords.latitude;
    location.latitude = position.coords.longitude;
}


/**
 *@description Returns the latitude of the current location
 *@return {float} a latitude
 */
function getLatitude() {
    return location.latitude;
}

/**
 *@description Returns the longitude of the current location
 @return {float} a longitude
 */
function getLongitude() {
    if (navigator.geolocation) {
        return location.longitude;
    }
}

function getCoordinates() {
    if (navigator.geolocation) {
        // https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition
        navigator.geolocation.getCurrentPosition(getLocation, function() {
            console.log("not able to get current position !!!!");
        });
    }
    else {
        console.log("browser doesn't support/allow geolocation !!!!");
    }
}

Open in new window

Massimo ScolaSoftware Engineer

Author

Commented:
yes, I had to change the sequence and I had a typo in it.
 
Thanks  a lot guys>!