0
function getMyLocation() {
if(navigator.geolocation){
    navigator.geolocation.getCurrentPosition(displayLocation);
}
else{
    alert("Geolocation not supported"); 
}
}

var user_lat;
var user_long;
var d_long;
var d_lat;

function displayLocation(position) { 
user_lat    = position.coords.latitude;
user_long   = position.coords.longitude; 
alert( "you are at lat: " + user_lat + " ,longitude: " + user_long);
 }

function getLocationDestination() {
var e = document.getElementById("building");
var strUser = e.options[e.selectedIndex].text;

alert("you want to go to " +  strUser + " and the cords are " + building_array[strUser]); 

getMyLocation();
alert("heelo");
displayLocation(position);
alert("heelo");
displayLocation(position);
}

代码非常简单,但是当我运行 geoLocationDestination() 时,即使在运行 20 次的 while 循环中,我也没有从 displayLocation(position) 收到第二个警报。程序在第二个 hello 后退出?有任何想法吗?

4

1 回答 1

0

当我运行你的代码时,我得到位置在 displaylocation 函数中是未定义的,这是因为你需要从 getMyLocation 调用这个函数而不是单独传递位置参数,下面是一个工作代码,

<html>
<script>
function getMyLocation() {
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(displayLocation);
}
else{
alert("Geolocation not supported"); 
}
}

 var user_lat;
 var user_long;
 var d_long;
 var d_lat;

function displayLocation(position) { 


 user_lat    = position.coords.latitude;
 user_long   = position.coords.longitude; 
 alert( "you are at lat: " + user_lat + " ,longitude: " + user_long);
 }

function getLocationDestination() {
var sel = document.getElementById("foo");
var option = sel.options[sel.selectedIndex];
var text = option.text;
var value = option.value;
alert(text, value);

getMyLocation();

}

</script>
<input type="submit" id=hello onclick=getLocationDestination();>   
<select id="foo">
<option value="1">One</option>
<option value="2">Two</option>
 <option value="3">Three</option>
 </select>

 </html>

此代码显示从选择框中选择的值并给出警报(我想它是你的编码的“你想去”的一部分,我不确定这一点,因为你没有包括 HTML 部分),然后提醒纬度和经度

于 2013-08-23T11:31:15.633 回答