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 8091573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:05:44+00:00 2026-06-05T20:05:44+00:00

I’m trying to make it so I can write a function which defines a

  • 0

I’m trying to make it so I can write a function which defines a variable, and then call that function within another function to give me the variable.

Here’s what I’ve done:

function getLat(){
  navigator.geolocation.getCurrentPosition(function(lat){
    var latCoord = lat.coords.latitude;
    return latCoord;
  });
}
function getLong(){
  navigator.geolocation.getCurrentPosition(function(lon){
    var longCoord = lon.coords.latitude;
    return longCoord;
  });
}
function getImage(){
  var the_lat = getLat(),
      the_lon = getLong();
  console.log(the_lat + ' ' + the_lon);
}
getImage();

As you can see I have the functions getLat(), and getLong() to grab the latitude, as well as the longitude of the person. Then, in getImage() I want to be able to call getLat() in a variable to assign latCoord to that variable. I was hoping return latCoord; would do this, but I was mistaken.

Is this possible, or should I be going about it a different way? If so, may I see an example of how I should be doing this?

Here’s a jsbin http://jsbin.com/ulelix/edit

Let me know if I need to clarify anything, or if you need me to add any information and I will do so as appropriate. I’ve searched around, but can’t seem to get the answer I’m looking for. Any help is appreciated, Thanks!

  • 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-06-05T20:05:45+00:00Added an answer on June 5, 2026 at 8:05 pm

    The navigator.geolocation.getCurrentPosition method is asynchronous. Your functions have returned undefined before the callbacks are executed.

    You need to move any code that relies on the response from that method into the callback.

    I would suggest combining your two functions into one, since both latitude and longitude are available in the callback:

    function getLatLng(){
        navigator.geolocation.getCurrentPosition(function(pos) {
            var lat = pos.coords.latitude,
                lng = post.coords.longitude;
    
            //Do stuff relying on lat/lng here...
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small

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.