python - Google map API 标记刷新

标签 python html google-maps gps refresh

我正在使用 Syrus GPS 调制解调器构建 GPS 定位服务的网站服务。我有两个文件,一个用于捕获来自调制解调器的信息的 python 代码,另一个是网页的 HTML 文件,python 脚本使用以下方法直接重写为 HTML:

 def replace_line(file_name, line_num, text):
    lines = open(file_name, 'r').readlines()
    lines[line_num] = text
    out = open(file_name, 'w')
    out.writelines(lines)
    out.close

我意识到为了更新 map 中的标记,我必须更新整个网页,这是不切实际的。

我使用了来自 google API 站点的示例:

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
    <script>
function initialize() {
var myLatlng = new google.maps.LatLng(21.0056,-75.8196);
  var mapOptions = {
    zoom: 15,
    center: myLatlng
  }
  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'Diseno 201510'
  });
}
google.maps.event.addDomListener(window, 'load', initialize);

    </script>

我只是用纬度和经度替换该线。那么有没有一种更简单的方法来更新标记而不刷新整个网页呢?

最佳答案

是的。但我不确定它有多容易,因为我不完全确定你首先是如何通过 Python 更新 map 的。 IE。您是否使用 websockets、重新渲染模板等...

当我被困在做同样的事情时,我使用了 Web 套接字(具体来说,socket.io)以及我的 Flask 应用程序。每隔“x”秒,我会将更新的纬度/经度坐标发送到站点,并使用 map.panTo(coordinates) 命令将 map 移动到新的位置坐标而不刷新页面。这是我用来执行此操作的 JavaScript 代码:

socket.on('newnumber', function(data){
    lat = data.number[0];
    lng = data.number[1];

    $('#locationText').text(data.number[3]);
    console.log("Lat: " + lat + " Long: " + lng);

    // Construct new LatLong Object 
    var myLatlng = new google.maps.LatLng(lng, lat);
    latLong = myLatlng;

    // Draw marker on the map 
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: data.number[3]
    });     
    // Move map to new coordinates w/o refreshing page
    map.panTo(latLong);
});

关于python - Google map API 标记刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28919482/

相关文章:

html - 将父 div 居中,而 width 检索子 div 的宽度值

javascript - 如何使用 grunt 更改我的 HTML 文件以引用缩小的 JS 和 CSS 而不是未压缩的文件?

javascript - 使用 Google Maps JS API 随机拖动/平移

javascript - 自定义谷歌地图 V.3 街景控件

javascript - 谷歌地图信息窗口关闭不起作用

python - 在python中使用字典用id替换字符串

python - 属性错误: 'tuple' object has no attribute 'client'

python - tensorflow :具有单个神经元的输出层,预期浮点输出[0.0,1.0]

python - Django - 呈现为字符串无法加载 CSS

python - 在 python 中浏览字符串