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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:24:29+00:00 2026-06-04T13:24:29+00:00

Today is my first day using the Google Maps API. Now I have a

  • 0

Today is my first day using the Google Maps API. Now I have a DIV with an id of “map” the following code…

function initializeMap() {

        var opts = {
            center: new google.maps.LatLng( 0, 0), // ignore the zero values for now
            zoom: 10,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map'), opts);
        geocoder = new google.maps.Geocoder();
}

and I call this using window.onload = initializeMap

Okay I’d like to center the map in my current location. I know I can get the long and lat using

navigator.geolocation.getCurrentPosition(displayLocation);

function displayLocation(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude; 
 }

Now I’m a little lost with how the position object is pushed back to success handler displayLocation. Anyway how can I pass the latitude & longitude to the initializeMap() function like so…

center: new google.maps.LatLng( latitude, longitude)

I’m currently looking at the Google Maps API docs as I’m sure there’s a method to get this but I’d like to use the latitude, longitude in different functions so I guess I’m asking how can I make these variables accessible to all functions (like a global but not a bad global) instead of being only available to the displayLocation function . Sorry if this is worded poorly… if I’m confusing people please say and I’ll reword this question.

  • 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-04T13:24:30+00:00Added an answer on June 4, 2026 at 1:24 pm

    just declare

    var latitude, longitude;
    

    into a parent scope, so the function is

    function displayLocation(position) {
        latitude = position.coords.latitude; /* no var keyword */
        longitude = position.coords.longitude; 
    }
    

    of course the choice of the scope in which you should define those variables is up to you. Also, be aware that they will be undefined until displayLocation is not called

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

Sidebar

Related Questions

Today is my first day using ASP.NET MVC, and I'm finding it very intriguing.
I have been using pil for the first time today. And I wanted to
First let me state, today is my first day using NSesrviceBus - so I
Today is the first day I'm looking into Ruby on Rails, and now I'm
I must confess that today is my first day using either Sweave or LaTeX.
This is literally my first day using PHP. I've never seen it until today,
Today's my first day with Flex and FlashDevelop. In my ActionScript file, I have
Please excuse me, today is my first day trying to setup a CI environment
Greetings. I am trying to learn Java and Swing (today is my first day).
First day at programming in Node today. I've been getting along nicely all day

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.