0

i have function like below

function showPosition(position) {
latOn = position.coords.latitude;
longOn = position.coords.longitude;
document.getElementById('lat').value=latOn;
document.getElementById('long').value=longOn;}

var locationx = new google.maps.LatLng(latO,longO);
latlng = locationx;

i want bring out "latOn and longOn" to

var locationx = new google.maps.LatLng('here');

help me please,... thnks before

feris
  • 9

1 Answers1

0

here you go: EDIT:

   var x = document.getElementById("demo");
var locationx = new google.maps.LatLng(-7.2574719, 112.7520883);
latlng = locationx;
var myLocation;
function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (position) {
      latOn = position.coords.latitude;
      longOn = position.coords.longitude;
      document.getElementById('lat').value = latOn;
      document.getElementById('long').value = longOn;
      myLocation = { latOn: latOn, longOn: longOn };
    }, showError);
  }
  else {
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

Now every change of the location the variable myLocation will be the new location

If you want getLocation to return a value use:

var x = document.getElementById("demo");
var locationx = new google.maps.LatLng(-7.2574719, 112.7520883);
latlng = locationx;
var myLocation = getLocation();
function getLocation() {
  if (navigator.geolocation) {
    var location;
    navigator.geolocation.getCurrentPosition(function (position) {
      latOn = position.coords.latitude;
      longOn = position.coords.longitude;
      document.getElementById('lat').value = latOn;
      document.getElementById('long').value = longOn;
      location = { latOn: latOn, longOn: longOn };
    }, showError);
  }
  else {
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
  if (location) {
    return location;
  }
}
Saar
  • 2,276
  • 1
  • 16
  • 14