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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:11:48+00:00 2026-05-22T16:11:48+00:00

I primarily code in PHP, I don’t have extensive knowledge on JavaScript scope; hoping

  • 0

I primarily code in PHP, I don’t have extensive knowledge on JavaScript scope; hoping somebody can solve my problem pretty quickly. As indicated by the comment, inspecting mapCenter.Latitude and mapCenter.Longitude – they appear empty.

The if will execute if location awareness is available in the browser – I’m certain it works for me, I tested it with an alert(). Furthermore, I know it is grabbing position.coords.latitude/longitude correctly, as I tested these with alert()’s too… But the values aren’t persistent outside of the function. This is probably trivial – what’s the fix?

function load(){
        map = new VEMap('MapDiv');  
        map.LoadMap();
        mapCenter = new VELatLong();
        if(navigator.geolocation)
        {
            navigator.geolocation.getCurrentPosition(function(position) 
            {
                mapCenter.Latitude = position.coords.latitude;
                mapCenter.Longitude = position.coords.longitude;

            });             
        }           

        //Inspecting mapCenter.Latitude & mapCenter.Longitude shows empty...

        map.SetCenterAndZoom(mapCenter, 15);
...
...
}

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-05-22T16:11:48+00:00Added an answer on May 22, 2026 at 4:11 pm

    getCurrentPosition accepts a callback which tells me that it is performing an asynchronous operation. So what is happening is that the code inside your anonymous function is most probably getting executed after map.setCenterAndZoom(mapCenter, 15) is called. When you work with asynchronous operations, execution proceeds past the asynchronous call without waiting for completion (hence asynchronous). So if you are depending on any data that comes from the asynchronous call, you need to make sure that you handle it within the callback, because it will most probably not be available to you otherwise.

    What you should do is make the call inside your callback like so:

    function load(){
            map = new VEMap('MapDiv');  
            map.LoadMap();
            mapCenter = new VELatLong();
            if(navigator.geolocation)
            {
                navigator.geolocation.getCurrentPosition(function(position) 
                {
                    mapCenter.Latitude = position.coords.latitude;
                    mapCenter.Longitude = position.coords.longitude;
                    map.SetCenterAndZoom(mapCenter, 15);
                    //any other code that needs to deal with mapCenter
                });             
            }           
    }
    

    map will be available inside the anonymous function since it behaves like a closure and so it is lexically bound to the scope in which it was defined.

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

Sidebar

Related Questions

I code primarily in javascript and in vb.net. I've found that if I can
I code primarily in PHP and Perl. I have a client who is insisting
I have a complicated problem, exacerbated by the fact I don't really know where
I have the following PHP code: $testMessage = TESTMESSAGE; $db = new SQLite3('messages.sq3'); $db->exec('CREATE
I am very new to the C# programming language. I code primarily in PHP/MySQL
I have following PHP code for inserting session data into a table called ds_session:
I have the following MySQL query, and PHP code to format the Count result
I code primarily in vb.net. I've been doing basic dependency injection manually and am
I am working on a software development project that uses code written primarily in
I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,

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.