如何实时更新Google地图折线?

时间:2017-06-19 18:05:54

标签: javascript google-maps real-time google-polyline

我试图在这里完成的工作是让来自thingpeak的gps cordinates并在google地图上显示它们。我已经能够在地图上只绘制一次折线和标记。当我尝试更新折线路径时,它会消失。

这是我的代码。

<script type="text/javascript">
    var map;
    var geocoder;
    var flightPath;
    var marker;

    function locate() {
        initMap();
        firstDraw();
    }

    //Initaite map
    function initMap() {
        var mapProp = {
            zoom: 12,
        };
        map = new google.maps.Map(document.getElementById("map-canvas"), mapProp);
        geocoder = new google.maps.Geocoder;
    }

    //draw for the first time
    function firstDraw() {
        $.getJSON("https://api.thingspeak.com/channels/284335/fields/1.json?api_key=D76FD3BOO9GNZBEY&results=10&location=true", function (result) {
            var i;
            var cordinates = [];
            var lastTime=result.feeds[result.feeds.length-1];
            for (i = 0; i < result.feeds.length; i++) {
                cordinates[i] = { lat: Number(result.feeds[i].latitude), lng: Number(result.feeds[i].longitude) };

            }

            flightPath = new google.maps.Polyline({
                path: cordinates,
                strokeColor: "#0000FF",
                strokeOpacity: 0.8,
                strokeWeight: 2
            });
            flightPath.setMap(map);
            map.setCenter(cordinates[cordinates.length - 1]);
            var marker = new google.maps.Marker({
                position: cordinates[cordinates.length - 1],
                map: map,
                title: "Last Seen",
            });
        });
    }

    function updateMap()
    {
        $.getJSON("https://api.thingspeak.com/channels/284335/fields/1.json?api_key=D76FD3BOO9GNZBEY&results=1&location=true", function (result) {
            var path = flightPath.getPath();
            var newCord = { lat: Number(result.feeds[0].latitude), lng: Number(result.feeds[0].longitude) };
            path.push(newCord);
            flightPath.setPath(path);
        });

    }
 </script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAfi_CDQTUC9waYxMwyJuED8DwoDJyl3F0&callback=myMap"></script>

我在这里做错了什么?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的问题是newCord不是google.maps.LatLng对象。

var newCord = { lat: Number(result.feeds[0].latitude), lng: Number(result.feeds[0].longitude) };
path.push(newCord);

getPath()检索MVCArraygoogle.maps.LatLng个对象。

来自the documentation

  

getPath()
  返回值:MVCArray
  检索路径。

虽然许多采用google.maps.LatLng对象的操作已经增强,可以采用google.maps.LatLngLiteral个对象,但在此处不起作用。改为使用google.maps.LatLng对象:

var path = flightPath.getPath();
var newCord = new google.maps.LatLng(Number(result.feeds[0].latitude), Number(result.feeds[0].longitude));

path.push(newCord);
flightPath.setPath(path);

proof of concept fiddle

代码段

html,
body,
#map-canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="map-canvas"></div>
<script type="text/javascript">
  var map;
  var geocoder;
  var flightPath;
  var marker;

  function locate() {
    initMap();
    firstDraw();
    setInterval(updateMap, 5000);
  }

  //Initiate map
  function initMap() {
    var mapProp = {
      zoom: 12,
      center: {
        lat: 0,
        lng: 0
      }
    };
    map = new google.maps.Map(document.getElementById("map-canvas"), mapProp);
    geocoder = new google.maps.Geocoder;
  }

  //draw for the first time
  function firstDraw() {
    $.getJSON("https://api.thingspeak.com/channels/284335/fields/1.json?api_key=D76FD3BOO9GNZBEY&results=10&location=true", function(result) {
      var i;
      var cordinates = [];
      var lastTime = result.feeds[result.feeds.length - 1];
      for (i = 0; i < result.feeds.length; i++) {
        cordinates[i] = {
          lat: Number(result.feeds[i].latitude),
          lng: Number(result.feeds[i].longitude)
        };
      }
      flightPath = new google.maps.Polyline({
        path: cordinates,
        strokeColor: "#0000FF",
        strokeOpacity: 0.8,
        strokeWeight: 2
      });
      flightPath.setMap(map);
      map.setCenter(cordinates[cordinates.length - 1]);
      var marker = new google.maps.Marker({
        position: cordinates[cordinates.length - 1],
        map: map,
        title: "Last Seen",
      });
    });
  }

  function updateMap() {
    $.getJSON("https://api.thingspeak.com/channels/284335/fields/1.json?api_key=D76FD3BOO9GNZBEY&results=1&location=true", function(result) {
      var path = flightPath.getPath();
      var newCord = new google.maps.LatLng(Number(result.feeds[0].latitude), Number(result.feeds[0].longitude));
      path.push(newCord);
      flightPath.setPath(path);
    });
  }
</script>
<script src="https://maps.googleapis.com/maps/api/js?callback=locate"></script>