E
E
Eugene2017-12-21 15:52:07
JavaScript
Eugene, 2017-12-21 15:52:07

How to pass variables from one chunk to another?

In general, there are two scripts. I stuffed them into functions

function myplace()
{
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var lat23 = position.coords.latitude;
            var lon23 = position.coords.longitude;
            alert("Ваши координаты" + " " + lat23+' '+lon23);


        });

    } else
    {
        alert("Geolocation API не поддерживается в вашем браузере");
    }
}




function geocoding()

{
    var my_adress;
    var api_key = 'AIzaSyAsuS1MqzRBzRv1HRrfrlyoMRlkrVXEx0g';
    var cordinats = [53.8652367,  27.650465399999998];
    var loctype = 'ROOFTOP';
    var restype = 'street_address';
    var position = cordinats.join(",");
    var data = {latlng: position, location_type: loctype, result_type: restype, key: api_key};
    $.ajax({
        method: "GET",
        url: "https://maps.googleapis.com/maps/api/geocode/json",
        data: data,
        dataType: 'json',

        success: function (result) {
            console.log(result)

            my_adress = result.results[0].formatted_address;
            alert("Вы здесь" + " " + "<" + " " + my_adress + " " + ">");
        },
        error: function (err) {
            console.log("Ошибка сервера")
        }


    })

}

I need the myplace() function to be initially executed, determine the coordinates, and then shove it into geocoding() ,
so that instead of var cordinats = [53.8652367, 27.650465399999998], these static coordinates are inserted lat23, lon23 coordinates

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Tarabrin, 2017-12-21
@evgen9586

function myplace()
{
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            geocoding(position.coords.latitude, position.coords.longitude);
        });
    } else
    {
        alert("Geolocation API не поддерживается в вашем браузере");
    }
}

function geocoding(lat, lon)
{
    var my_adress;
    var api_key = 'AIzaSyAsuS1MqzRBzRv1HRrfrlyoMRlkrVXEx0g';
    var cordinats = [lat,  lon];
    var loctype = 'ROOFTOP';
    var restype = 'street_address';
    var position = cordinats.join(",");
    var data = {latlng: position, location_type: loctype, result_type: restype, key: api_key};
    $.ajax({
        method: "GET",
        url: "https://maps.googleapis.com/maps/api/geocode/json",
        data: data,
        dataType: 'json',
        success: function (result) {
            console.log(result)
            my_adress = result.results[0].formatted_address;
            alert("Вы здесь" + " " + "<" + " " + my_adress + " " + ">");
        },
        error: function (err) {
            console.log("Ошибка сервера")
        }
    })
}
myplace(); // Вызываем всё это дело.

E
Evgeny Kalibrov, 2017-12-21
@rework

function myplace()
{
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var lat23 = position.coords.latitude;
            var lon23 = position.coords.longitude;
            return [lat23, lon23];


        });

    } else
    {
        alert("Geolocation API не поддерживается в вашем браузере");
    }
}




function geocoding()

{
    var my_adress;
    var api_key = 'AIzaSyAsuS1MqzRBzRv1HRrfrlyoMRlkrVXEx0g';
    var cordinats = myplace();
    var loctype = 'ROOFTOP';
    var restype = 'street_address';
    var position = cordinats.join(",");
    var data = {latlng: position, location_type: loctype, result_type: restype, key: api_key};
    $.ajax({
        method: "GET",
        url: "https://maps.googleapis.com/maps/api/geocode/json",
        data: data,
        dataType: 'json',

        success: function (result) {
            console.log(result)

            my_adress = result.results[0].formatted_address;
            alert("Вы здесь" + " " + "<" + " " + my_adress + " " + ">");
        },
        error: function (err) {
            console.log("Ошибка сервера")
        }


    })

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question