Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:32:27+00:00 2026-05-28T01:32:27+00:00

I have the following JavaScript function which receives coordinates and returns the nearest tube

  • 0

I have the following JavaScript function which receives coordinates and returns the nearest tube station:

    function coord() {

                var metro = new YMaps.Metro.Closest(new YMaps.GeoPoint(<?=getCoords($addr) ?>), { results : 1 } )

                YMaps.Events.observe(metro, metro.Events.Load, function (metro) {
                    if (metro.length()) {
                        metro.setStyle("default#greenSmallPoint");
                        var firstStation = metro.get(0);
                        var tubest = (firstStation.text).split("метро ");
                            var tube = tubest[1];
                        if($("span#tubest").text() == '') {
                            $('.whiteover').hide();
                        }
                    } else {
                        if($("span#tubest").text() == '') {
                            $('.whiteover').hide();
                        }
                    }
                });
}

The value which I need to output as a result of this function execution is the value of the “tube” variable (var tube = tubest[1];). Basically a simple document.write will work. Or a simple return value like:

var tubestation = coord();

However I’m not sure how to achieve this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:32:28+00:00Added an answer on May 28, 2026 at 1:32 am

    You can’t have this function return the value, since you’re using an observer pattern – which sets up an asynchronous logic to the code. Simply saying, at the time that your coord() function returns, the value is not there yet.

    To deal with this, normally you would pass a callback function, then resume your computation there.

    Declare your function as:

    function coord(callback)
    

    then, after you know the value you want, call the callback with the value:

    callback.call(null, tube);
    

    Do it after your if { ... } else { ... } so your callback gets called both on success and on failure (on failure it will pass undefined, you might want to correct it by declaring var tube = null before the if).

    then, instead of:

    tubestation = coord();
    

    call it like this:

    coord(function(tubestation) {
      // continuation of your code here
    });
    

    You probably won’t be able to use document.write since the time to use it would be long past, but you can set the value as the contents of an element that you already generated. You have jQuery in your tags, so it’s quite easy:

    coord(function(tubestation) {
      $('#tube_station').text(tubestation);
    });
    

    assuming you have <div id="tube_station"/> somewhere in your HTML.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following javascript function which creates a new row with different cells
I have the following JavaScript code: Link In which the function makewindows does not
I have the following javascript code, which loads without error, however the update function
I have the following javascript function which i call on the submit button click....
I have the following JavaScript code which is inside a function which I am
I have the following javascript function hide(id) { var ele = document.getElementById(id); if ((ele.style.display
I have the following JavaScript (I'm using jQuery): function language(language) { var text =
I have the following Javascript code: function checkIfValid(){ var form = document.createuserform; if(form.fName.value ==
I have the following function in javascript that calls the ajax which calls the
I have the following javascript function, which uses .append from JQuery. However it only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.