从javascript函数中提取变量

时间:2015-09-28 10:35:30

标签: javascript

我的功能如下

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;

我想把“latOn和longOn”带到

var locationx = new google.maps.LatLng('here');
请帮助我,... 先于

1 个答案:

答案 0 :(得分:0)

你去:编辑:

   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.";
  }
}

现在变量myLocation的所有位置变化都是新位置

如果您希望getLocation返回值,请使用:

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;
  }
}