AQI 使用 Google Maps API JavaScript 监控实时地图并从 Thingspeak .getJSON 和 Marker 读取 JSON

问题描述 投票:0回答:1

我正在从事一项工作,我们正在教高中生制作空气质量监测仪。所有 AQ 监测器都会将其纬度、经度、AQI 和温度数据发送到 Thingspeak。我想显示每台显示器的位置及其最新的 AQI 编号。我在 github 上找到了使用经纬度绘制数据点的代码,但它没有指定如何将 AQI 添加为标记的标签。可以做这样的事情吗?仅供参考,100 个监视器中的每一个都有自己的频道。

<!DOCTYPE html>
<html>
  <head>
    <title>Experiment</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>



    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>




    <script>
      // Initialize global variables



      // Initialize and add the map
      function initMap() {
        // get the last location sent 

        // get LATITUDE
        $.getJSON("https://api.thingspeak.com/channels/CHANNELID/fields/1/last.json?api_key=THINGSPEAKAPIKEY", function(result){

          var m = result;
          // set latitude as x
          x=Number(m.field1);

        });

        // get LONGITUDE
        $.getJSON("https://api.thingspeak.com/channels/CHANNELID/fields/2/last.json?api_key=THINGSPEAKAPIKEY", function(result){

          var m = result;
          // set longitude as y
          y=Number(m.field2);

          $.getJSON("https://api.thingspeak.com/channels/CHANNELID/fields/6/last.json?api_key=THINGSPEAKAPIKEY", function(result){

          var m = result;
          // set label as label
          label=String(m.field6);

        });

        }).done(function (){

          coordinates = {lat: x, lng: y};


          // The map, centered at the location given
          var map = new google.maps.Map(
              document.getElementById('map'), {zoom: 13, center: coordinates});
          // The marker, positioned at the location given
          var marker = new google.maps.Marker({
            position: coordinates, 
            map: map,
            title: 'Click to get coordinates',
            label: label,
          });

          var infowindow = new google.maps.InfoWindow({
              content: '<p>Tracker Location: ' + marker.getPosition() + '</p>'
          });

          map.addListener('center_changed', function() {
            // 3 seconds after the center of the map has changed, pan back to the
            // marker.
            window.setTimeout(function() {
              map.panTo(marker.getPosition());
            }, 5000);
          });

          marker.addListener('click', function() {
            infowindow.open(map, marker);
            // when the marker is clicked, center map
            map.setCenter(marker.getPosition());
          });
        });





      }

    </script>
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=GOOGLEAPIKEY&callback=initMap"></script>

  </body>
</html>

我尝试添加另一个 getJSON 以从字段 6(我的 AQI 列)中提取值。我试图在标记下添加标签:标签,但这不起作用。如何从 field6 添加标签?我认为数据类型可能是问题所在,但我将其更改为 String,但仍然无法正常工作。这是错误的做法吗?

javascript json google-maps google-maps-api-3 google-maps-markers
1个回答
0
投票

经纬度字段未包含在 thingspeak 的 json 中。我从 AQ 监视器手动添加经纬度,然后使用 .getjson 调用具有 AQI 的 field6。我必须将该字段转换为字符串,然后添加标签:x,才能使其正常工作。现在我必须弄清楚如何在不重复代码的情况下对所有 100 个执行此操作。欢迎任何建议。

© www.soinside.com 2019 - 2024. All rights reserved.