0
votes

I want to get accuracy value from inside the function and create if else statement with the accuracy. I don't understand how to get the accuracy value inside the function and pass it to outside of the function. This is my code:

function initMap(position) {

    var latitude = position.coords.latitude,
        longitude = position.coords.longitude,
        accuracy = position.coords.accuracy;

    var mapOptions = {
        zoom: 18,
        center: {
            lat: latitude,
            lng: longitude,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        },
    }
}
2
Look up JavaScript returns and parametersLoaf

2 Answers

1
votes

Also, you can create a global variable, outside the function. and instead of declaring "accuracy" as a local variable, just use the global variable inside the function like so:

var accuracy;

function initMap(position) {

    var latitude = position.coords.latitude,
        longitude = position.coords.longitude;

    accuracy = position.coords.accuracy

    var mapOptions = {
        zoom: 18,
        center: {
            lat: latitude,
            lng: longitude,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        },
    }
}

Then "accuracy" will be available to everything within the script. To indicate a global variable, sometimes people will use "g" or "_" (underscore), like so:

var _accuracy;
var gAccuracy;

Edit: Bad grammar/typo.

0
votes

Simple! add a return accuracy; at the end of your function. Then, you can set a variable to that value, for example var random = initMap(position);. This will set random to accuracy.