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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:00:12+00:00 2026-05-31T03:00:12+00:00

I have a serious problem that i have been trying to debug for a

  • 0

I have a serious problem that i have been trying to debug for a few days already.
I have a script that gets users current latitude and longitude, and then stores them in variables. however, when i try to use these variables outside this function and in the //init map zone, the map is just not showing up. by alerting out the variables i can see that outside the position function variables are set to “Undefined”. here is my code:

//main function here
function initialize() {
var lat;
var lon;

//check if user has geo feature
if(navigator.geolocation){

navigator.geolocation.getCurrentPosition(
//get position
function(position){
    lat = position.coords.latitude;
    lon = position.coords.longitude;
    },
// if there was an error
function(error){
    alert('ouch');
});
}
//case the users browser doesn't support geolocations
else {
alert("Your browser doesn't support geolocations, please consider downloading Google Chrome");
}
//init map
var myOptions = {
    center: new google.maps.LatLng(lat, lon),
    zoom: 16,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    };
var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

}

Thanks for any help, Ariel

  • 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-31T03:00:13+00:00Added an answer on May 31, 2026 at 3:00 am

    That is because you declare your variables within the function. The variables gets private to the initialize function and can only be accessed from within it. If you need to be able to access your variables outside of the initialize function, then move the variable declaration out of the function.

    var lat;
    var lon;
    function initialize() {
    ...
    

    Have a look at this MDN article about variable scope in JavaScript.

    UPDATE

    Looking through the code again I realize that the problem isn’t variable scope, got confused by the indentation. I’m not familiar with the Geolocation API but I believe that the problem might be that the navigator.geolocation.getCurrentPosition() is asynchronous, as it will have to wait for the user to allow the website to get the position of the device. Therefor myOptions will have been assigned before the actual position have been retrieved – thus lat & lng are still undefined when myOptions is assigned.

    Try this instead:

    //main function here
    function initialize() {
    var lat, lon, map, myOptions;
    
    //check if user has geo feature
    if(navigator.geolocation){
    
    navigator.geolocation.getCurrentPosition(
    //get position
    function(position){
        lat = position.coords.latitude;
        lon = position.coords.longitude;
    
        //init map
        myOptions = {
           center: new google.maps.LatLng(lat, lon),
           zoom: 16,
           mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"),
            myOptions);
    },
    // if there was an error
    function(error){
        alert('ouch');
    });
    }
    //case the users browser doesn't support geolocations
    else {
    alert("Your browser doesn't support geolocations, please consider downloading Google Chrome");
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Actually I have a serious problem : I saw that somebody has accessed my
I got a serious problem here: I have a scrolling background that is 1320
I have a serious problem. Firstly, I am trying to retrieve values from my
Here's the situation: I have a web-based ticket application, multiple users. One problem that
I have a serious problem. One of our servers crashed. Now I reinstalled the
Ok guys, I have a serious problem with this. I have a static class
I have a serious performance problem. I have a database with (related to this
I have a serious problem in my app. I wrote a facebook like instant
We have been trying now for a while to assist the management (of a
I have been using PHP for a while, but now I am trying to

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.