【问题标题】:Javascript HTML5 Geolocation bug?Javascript HTML5 地理定位错误?
【发布时间】:2013-08-23 10:44:13
【问题描述】:
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 后退出?有任何想法吗?

【问题讨论】:

  • 看来positiongetLocationDestination() 中未定义...

标签: javascript compass-geolocation


【解决方案1】:

当我运行你的代码时,我得到位置在 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 部分), 然后提醒经纬度

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 2015-03-27
    相关资源
    最近更新 更多