繁体   English   中英

将garmin gps坐标转换为javascript变量

[英]get garmin gps coordinates into javascript variable

我想删除一个标记并在openlayers(javascript)中创建一个新标记。 我可以做到这一点,但诀窍是我想把新标记放在我的garmin etrex gps接收器报告的当前纬度/经度位置。 我可以使用gpsbabel从接收器获取新位置,我可以使用子进程在python中执行此操作。 我能想到将这些数据转换为javascript变量的唯一方法是在python CGI脚本中使用字符串替换。 问题是从gps接收器获取新数据的唯一方法是刷新页面,这会导致地图短时间消失,当前缩放级别丢失,而且它不是一个非常干净的方式这样做。 我正在寻找任何关于如何从gps获取坐标到javascript变量的想法。

jython会工作,从python脚本到javascript的坐标? 我不熟悉它,但我今天所做的读数似乎表明这需要在端口8080上运行,这是不理想的,因为我想使用python cgi脚本。 也许这仍然是可能的。

我已经找了api,但是garmin的产品似乎与他们的网站有关,如果你只想要当前的坐标,它们就太过分了。

我看了gpsd,但是在基本通信方面遇到了麻烦,而且无论如何都无法通过tcp端口2947从javascript中找到与守护进程通信。

我正在使用gentoo linux。

谢谢,约翰

在客户端设置一个计时器(使用setTimeout javascript函数)每10秒左右更新一次标记位置。 为此你应该使用ajax从服务器(运行python代码或类似的东西)获取最后一个位置。

所以,正如@sahmad建议的那样,看起来AJAX是最佳选择。 就像我之前说的那样,我开始试图用浏览器插件解决问题。 我为此使用了firebreath。 在此过程中学到了几个艰苦的经验教训。 最重要的是,当您对插件进行更改时,您需要崩溃插件并重新加载页面或重新启动浏览器。 我浪费了很多时间对这个简单的事实一无所知。 这个插件的方向对我来说也需要更长的时间,因为我的C ++技能处于开发的早期阶段。

AJAX路线非常简单易学。 我用这个页面作为我的起点: http//www.degraeve.com/reference/simple-ajax-example.php

我想我会完成我的例子并报告我的解决方案,然后这件事情全部完成了。 但是,在创建我的例子的过程中,我提出了另一个问题。 这是文件:

我这样修改了html文件:

<html>
<head>
<title>Simple Ajax Example</title>
<script language="Javascript">
function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(getquerystring());
}

function getquerystring() {
    qstr = 'latLon=' + escape(document.forms['f1'].lat.value + ':' + document.forms['f1'].lon.value);
    return qstr;
}

function updatepage(str){
    document.getElementById("result").innerHTML = str;
}

function moveMarker(){
    document.getElementById("timer").innerHTML= parseFloat(document.getElementById("timer").innerHTML) + 1
    xmlhttpPost("/cgi-bin/ajax3.py")

    var latFromGPS = document.forms["f1"]["gpsLat"].value;
    var lonFromGPS = document.forms["f1"]["gpsLon"].value;

    document.forms["f1"]["lat"].value=latFromGPS;
    document.forms["f1"]["lon"].value=lonFromGPS;

}    
window.setInterval('moveMarker()', 2000);

</script>
</head>
<body>
<form name="f1">
    <p id=timer>0</p>
    <p><input type="text" id="lat" name="lat" value="35.0"> </p>
    <!-- <input type="hidden" name="gpsLat" value="35.0"> -->
    <p><input type="text" id="lon" name="lon" value="-106.0"> </p>
  <div id="result"></div>
</form>
</body>
</html>

这是我的python cgi脚本(对于这个例子,它应该保存为cgi-bin中的ajax3.py):

#! /usr/bin/python

import cgi

import cgitb; cgitb.enable() # for troubleshooting

print "Content-type: text/html"
print ""

# Create instance of FieldStorage 
form = cgi.FieldStorage()

gpsLat = float(form.getvalue("latLon").split(":")[0])
gpsLon = float(form.getvalue("latLon").split(":")[1])

print '<input type="hidden" name="gpsLat" value="%s">' % (gpsLat + 0.001)
print '<input type="hidden" name="gpsLon" value="%s">' % (gpsLon + 0.001)

我试图通过简单地将cgi脚本中的纬度和经度加到千分之一来模拟GPS报告坐标。 然后通过隐藏的输入类型将此结果发送回原始页面。 有趣的是,我基本上必须调用moveMarker()的频率是我想要的坐标报告的两倍。 这不是问题,但我想理解为什么会这样。 从我天真的角度来看,我认为

xmlhttpPost("/cgi-bin/ajax3.py") 

命令将在下一个命令之前执行并完成

var latFromGPS = document.forms["f1"]["gpsLat"].value;

被执行了。 然而,情况并非如此。 那么第一个命令只在一定时间后完成,还是在moveMarker()完成之后才完成?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM