0

Realistically I just want a way to access the latitude and longitude later like a global variable. I came up with this 'solution' if you can call it that. It's been a while since i've done some OOP.

What do I need to do?

var geo = {

   local: function() {
    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(postition){
      latitude: position.coords.latitude;
      longitude: position.coords.longitude;
    })
  }
}
};

function initMap() {
  var userLocation = {lat: geo.local.latitude, lng: geo.local.longitude};
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom:14,
    center: userLocation
  });
};

console.log(geo.local.longitude);
console.log(geo.local.latitude);

Thanks!

Quesofat
  • 1,493
  • 2
  • 21
  • 49

2 Answers2

1

You could do something like this :

var geo = {
  local: {
    longitude: "",
    latitude: "",
    positionFound: false
  },
  location: (function() {

    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        geo.local.latitude = position.coords.latitude;
        geo.local.longitude = position.coords.longitude;
        geo.local.positionFound = true;
      });
    }
  })()
};

function initMap() {
  var userLocation = {
    lat: geo.local.latitude,
    lng: geo.local.longitude
  };
  if (geo.local.positionFound) {
    console.log(userLocation.lat + " - " + userLocation.lng);
  } else {
    console.log("Location Not found");
  }
};

setTimeout(function() {
  initMap();
}, 5000);

A working snippet is on jsFiddle, as code snippet doesn't seem to allow location access.

Sreekanth
  • 3,110
  • 10
  • 22
  • i would love it if you were able to walk me through it real quick! – Quesofat Oct 16 '16 at 01:27
  • 1
    basically, it would have been a simple solution, had the geolocation api is just a synchronous method. Due to the fact that getCurrentPosition is an asynchronous method. We do need to wait for the method to complete and success callback is executed. so someone has to invoke the Async api and thats when IIFEs come handy. They invoke the method immediately after declaration. geo.location is an example of IIFE. But as per your ask, you wanted position to be available as a variable, we need to update them in some properties where we can access. – Sreekanth Oct 16 '16 at 01:36
  • 1
    Hence geo.local. Also, we need to ensure the position has been filled properly, we need to be sure, that getCurrentPosition is executed successfully and hence use the locationFound as a gatekeeper to ensure we have the right position values. this could be handy , when you want to be execute something only when the position values are found. In your case, creating google maps object. – Sreekanth Oct 16 '16 at 01:38
0

You can have many ways, like:

1) Localstorage - How to set session variable in jquery? ; http://www.w3schools.com/html/html5_webstorage.asp

2) Session plugin - https://github.com/AlexChittock/JQuery-Session-Plugin

3) Data plugin - https://api.jquery.com/data/

4) Custom data attributes - http://html5doctor.com/html5-custom-data-attributes/

Community
  • 1
  • 1
Almeida
  • 1,254
  • 12
  • 26