Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript HTML5地理位置错误?_Javascript_Compass Geolocation - Fatal编程技术网

Javascript HTML5地理位置错误?

Javascript HTML5地理位置错误?,javascript,compass-geolocation,Javascript,Compass Geolocation,代码非常简单,但当我运行geoLocationDestination()时,我不会从displayLocation(position)收到第二个警报,即使在运行了20次的while循环中也不会。程序在第二个hello之后退出?有什么想法吗 当我运行您的代码时,我得到的位置在displaylocation函数中是未定义的,这是因为您需要从getMyLocation调用此函数,而不是单独传递位置参数, 下面是一个工作代码 function getMyLocation() { if(navigator

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

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

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

函数getMyLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(displayLocation);
}
否则{
警报(“不支持地理位置”);
}
}
var user_lat;
var用户_long;
var d_long;
var d_lat;
功能显示位置(位置){
用户纬度=位置坐标纬度;
user_long=position.coords.longitude;
警报(“您在纬度:“+user\u lat+”,经度:“+user\u long”);
}
函数getLocationDestination(){
var sel=document.getElementById(“foo”);
var option=sel.options[sel.selectedIndex];
var text=option.text;
var值=期权价值;
警报(文本、值);
getMyLocation();
}
一个
两个
三
此代码显示从选择框中选择的值并发出警报(我想这是编码的“you want to go”部分,我不确定,因为您没有包含HTML部分),
然后警告纬度和经度

似乎
位置
getLocationDestination()
中未定义。。。
<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>