2

Im a javascript learner and have some trouble with my script i hope someone has time to help me. In short I want the script to get position of user, save it to a variable and send the position of an object to the server.

I have 3 different objects in my actual script. I want to assign a position to the object before sent to server. I know this can be solved more elegant than having 3 functions (getPositionobject1, 2, 3) that all contain the same if statement. But this is a good starting point.

This script run OK, but sendToServer get called before geometry/position has been assign. Any advice of how i can arrange my code to make sure sendToServer() is called after geometry is assigned?

Here is a fiddle https://jsfiddle.net/mw7cgwwz/22/

function getPositionobject1() {
    if (navigator.geolocation) {
          var timeoutVal = 10 * 1000 * 1000;
          navigator.geolocation.getCurrentPosition(
            displayPosition,
            displayError,
            { enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
          );
          sendObjectOneToServer(); //This get called before geometry is defined 
}


function displayPosition(position) {
  alert("Latitude: " + position.coords.latitude + ", Longitude: " + position.coords.longitude)
  geometry = (position.coords.latitude + "," + position.coords.longitude).toString()
}


sendToServer() {
    var postData =
        'stuff' + geometry
}

1 Answer 1

1

The first and second parameter of Geolocation.getCurrentPosition() are callback functions, they are called when the method has finished (successfully or not).

Create an anonymous function for you success-Callback, and put it in your "displayPosition"-method, that is called with the object as an argument:

function getPositionobject1(objectid) {
    if (navigator.geolocation) {
        var timeoutVal = 10 * 1000 * 1000;
        navigator.geolocation.getCurrentPosition(
            function(position) {displayPosition(position, objectid)},
            displayError,
            { enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
        );
    }
}

Here's a fiddle

(Instead of a string, you could pass the object itself)

5
  • Unfortunately I cannot do that,because I have 3 different objects, I need to define what object that are going sent to the server. In my case its defined by the function name getPositionobject1() I know the code is redundant but I will have to have 3 functions one for each object. Im sure there is a better way of doing it, but im not sure how
    – geogrow
    Commented Oct 12, 2016 at 15:25
  • Updated. It is messy i know. In my case as it is now. getPositionobject1() get position of the object1 and send it to the server. I hope it make more sence
    – geogrow
    Commented Oct 12, 2016 at 15:32
  • The fiddle is still the same as before. Did you click "update"?
    – Greg Z
    Commented Oct 12, 2016 at 15:34
  • Sorry you are right, did not realize the link to the fiddle changed after each update. I updated the link now
    – geogrow
    Commented Oct 12, 2016 at 15:36
  • Yes thank you! That definitely put me back on track. Kind of what I was hoping for, to reuse the code better.
    – geogrow
    Commented Oct 12, 2016 at 16:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.