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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:19:01+00:00 2026-05-20T11:19:01+00:00

I am able to get the lat/lng coordinates, but somehow the system sets the

  • 0

I am able to get the lat/lng coordinates, but somehow the system sets the center point of the map to (0,0). Would anyone be able to point me in the right direction for how to get the right lat/lng to become my center points?

The url where I am testing this is here:
http://www.comehike.com/hikes/hike_carpool.php?hike_id=125

Thank you,
Alex

  • 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-20T11:19:02+00:00Added an answer on May 20, 2026 at 11:19 am

    just looked at your code:

    var lat = 0;
    var lng = 0;
    
    request.onreadystatechange = function()
    {
        ...
        lat = parseFloat(markers[i].getAttribute("lat"));
        lng = parseFloat(markers[i].getAttribute("lng"));
        ...
    } // Closing onReadyStateChange
    
    request.send(null);
    
    var myLatlng = new google.maps.LatLng(lat , lng ); // Have to make the center as the hike coordinates.
    var myOptions =
    {
        zoom: 9,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    

    map is set to (0,0) because you defined lat, lng so, looks like you rely on lat, lng changing in ajax callback function, but it is asynchronous, so when browser gets to

    var myLatlng = new google.maps.LatLng(lat , lng );
    

    lat and lng in 99.99% cases will be 0, and only after some time (depending on network etc.) they will be changed, but it will be after map is built… Otherwise you have to create your map in callback function:

    function createMap( lat, lng ) 
    {
       var myLatlng = new google.maps.LatLng(lat , lng ); // Have to make the center as the hike coordinates.
        var myOptions =
        {
            zoom: 9,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        return new google.maps.Map(document.getElementById("map_canvas"), myOptions);    
    }
    
    function initialize( hike_id ) {
      var lat = 0;
      var lng = 0;
      var map;
    
      request.onreadystatechange = function()
      {
          ...
          lat = parseFloat(markers[i].getAttribute("lat"));
          lng = parseFloat(markers[i].getAttribute("lng"));
          ...
          map = createMap( lat, lng );
    
       } // Closing onReadyStateChange
    
       request.send(null);
    }
    

    shortly, you have to be sure lat and lng are defined as you need before map creation… This way it should work. Alternatively you can create map with center in (0,0) or somewhere else, but predefined, and then in ajax callback use map.setCenter( center_latlng );

    also you do not create markers… your callback has

    // create the marker
    center_latlng = new google.maps.LatLng( lat , lng ); 
    global_markers[i] = marker;
    

    you define center_latlng value, but do not use it.. instead you put marker defined outside of callback to your markers array. Should be something like:

    // create the marker
    center_latlng = new google.maps.LatLng( lat , lng ); 
    var hikeMarker = new google.maps.Marker({
       position: center_latlng,
       map: map,
       title:"Hike Start"
    }); 
    global_markers[i] = hikeMarker ;
    

    EDIT: Took a look at your last implementation:

    function createMap( center_lat , center_lng ) {
       ...
       map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
       ...
    }
    

    that function creates map in global scope, that’s not good until you really need it to be global. I updated my code above, so I would return map in that function (have a look). Otherwise, if you really need map to be global (meaning that something else could use it), then I would prefer another way (was shortly told above too): create map with center anywhere, then set its center in ajax callback. This way you will be sure that map is created as soon as possible, and you won’t get in trouble with “null” value (again if it is used outside initialize method, of even out of ajax callback:

    function createMap( lat, lng ) 
    {
       var myLatlng = new google.maps.LatLng(lat , lng ); // Have to make the center as the hike coordinates.
        var myOptions =
        {
            zoom: 9,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        return new google.maps.Map(document.getElementById("map_canvas"), myOptions);    
    }
    
    function initialize( hike_id ) {
      var lat = 0;
      var lng = 0;
      var map = createMap( lat, lng ); // create with center in (0,0); probably would be better to use San Francisco
    
      request.onreadystatechange = function()
      {
          ...
          lat = parseFloat(markers[i].getAttribute("lat"));
          lng = parseFloat(markers[i].getAttribute("lng"));
          ...
          map.setCenter( lat, lng ); // it will change position of map to real one
    
       } // Closing onReadyStateChange
    
       request.send(null);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has anyone been able to get an NHibernate-based project up and running on a
Has anyone been able to get xinc to run correctly under OpenBSD's chrooted default
Has anyone been able to get a variable record length text file (CSV) into
I'm able to get cells to format as Dates, but I've been unable to
Has anyone been able to get Windows Installer to use the InstallUISequence table during
I was able to get this working is SSRS 2008, but do to the
Is it possible to get the Lat & Lng without going outdoor? My current
I am not able to find out how to get lat-lon values of all
I searched a lot in google but not able to get the answer required.
I need to be able get a single specific attribute from an element with

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.