我使用javascript获取当前位置(LngLat) ;
并使用全局变量来保存Lng或Lang ..
它在function show();
内部发生了变化,但是当它返回到1后,y
没有变化。
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
var y=1;
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(show);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function show(p) {
y=p.coords.latitude;
//window.alert(y);
x.innerHTML = "Latitude: " + p.coords.latitude +
"<br>Longitude: " + p.coords.longitude;
}
window.alert(y);//<-- here is the problem , I don't want it to alert 1 !
答案 0 :(得分:0)
看到这一点:
function show(p) {
y=p.coords.latitude;
//window.alert(y);
x.innerHTML = "Latitude: " + p.coords.latitude +
"<br>Longitude: " + p.coords.longitude;
startApp();
}
function startApp(){
window.alert(y);
// do whatever else you want
}