Google Maps API - Trying to insert initial coordinate values from page element IDs dynamically

I have a store locator that pulls the nearest location (based on the user location) in the header section.

On the homepage, there is an additional section that shows the Google Map with the closest store information and the map as the background that is supposed to center the map on the store coordinates.

I am unable to get this to work, but have tried to match the latitude and longitude values to be pulled based on the ID's of the fields that hold the coordinates for the active store in the header.

function showGoogleMaps() {

map = new google.maps.Map(document.getElementById('googlemaps'), {
      center: {lat: 43.6570403, lng: -79.4609326},
      zoom: 14
    });


if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
     var pos = {
         lat: position.coords.latitude,
      lng: position.coords.longitude
     };
     //       lat: document.getElementById("show_lat").innerText,
    //  lng: document.getElementById("show_lng").innerText
     map.setCenter(pos);
      });
    }
  }

Open in new window


The show above shows the current user location (when shared) and centers the map on those coordinates.

The commented code is what I tried to pull the values into the lat/lng values and center the map on that location.

To add, the ID's in question also have the data-value specified based on the store that holds the coordinates for LAT and LNG.

Any info would be very helpful, thanks!
LVL 5
Branislav BorojevicWeb EnthusiastAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael VasilevskySolutions ArchitectCommented:
Have you tried converting the string from innerText to float?

function showGoogleMaps() {

  map = new google.maps.Map(document.getElementById('googlemaps'), {
    center: {
      lat: 43.6570403,
      lng: -79.4609326
    },
    zoom: 14
  });

  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = {
        lat: parseFloat(document.getElementById("show_lat").innerText),
        lng: parseFloat(document.getElementById("show_lng").innerText)
      };
      map.setCenter(pos);
    });
  }
  
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Google

From novice to tech pro — start learning today.