0

i am trying to get the GPS location of a user for all my services i have created a separated file let Say Factory.js

This is the code in my factory file:

var get_my_location=function(){

        var onSuccess = function(position) {
        if(typeof position ==='object'){

            return position
        }
        else{
            return 500;
}
    };

    // onError Callback receives a PositionError object
    //
    function onError(error) {
       return {"err":error}
    }

    navigator.geolocation.getCurrentPosition(onSuccess, onError);
    }

i want to call and get the position in response when any other function calls this get_my_location();

When i am trying like:

var resp=get_my_location();
console.log(resp) //outpur 'undefined'

obviously it should give undefined,how can i apply a callback so that i may get the position in resp as a returned value.

anujayk
  • 544
  • 7
  • 31

1 Answers1

0

Below is how you can pass callback. Below is a link where you can get more information.

Getting a better understanding of callback functions in JavaScript

function vinoth() {
    var obj = {};
    obj.name = "Kalam";
    return obj;
}

function callFunction(func) {
    var newValue = func();
    console.log(newValue);
}

callFunction(vinoth);
Community
  • 1
  • 1
Thalaivar
  • 23,282
  • 5
  • 60
  • 71
  • how if my function vinoth is returning some object and i wan to print it as a return value of callFunction ? – anujayk Jan 07 '16 at 11:38