【问题标题】:Preview of polyline drawing each coordinate by coordinate in google maps在谷歌地图中按坐标预览折线绘制每个坐标
【发布时间】:2015-03-10 09:53:33
【问题描述】:

我在 googlemap 中使用折线创建了一个应用程序,该应用程序在完美绘制折线的情况下工作正常,但问题是我需要绘制并显示折线的预览,按坐标绘制每个坐标谷歌地图

我们怎样才能做到这一点?我已经尝试使用 setInterval(在我的代码中注释)但它不能正常工作

谁能告诉我一些解决方法

      $scope.autoRefresh = function() {
    try {
      //
      routePoints = [];
      if (!_.isEmpty(items)) {
        angular.forEach(items, function(cordinates) {
          //setInterval(function ()
          //{           
            routePoints.push(new google.maps.LatLng(cordinates.lat, cordinates.lng));
            var route = new google.maps.Polyline({
              path: routePoints,
              strokeColor: '#FF0000',
              strokeOpacity: 2.0,
              strokeWeight: 3,
              editable: false
            });
            route.setMap(map);
            moveMarker(map, marker, cordinates.lat, cordinates.lng);
            markLAT = cordinates.lat;
            markLNG = cordinates.lng;
          //}, 1000);
        });  
      }
      //  
    } catch (e) {
      console.log(e);
    }
  };

Plunker

【问题讨论】:

标签: javascript angularjs google-maps google-maps-api-3 google-maps-markers


【解决方案1】:
  1. setInterval不是正确的方法,必须使用setTimeout,否则函数会无限运行(直到浏览器崩溃)

  2. 您必须增加 setTimeout 的延迟,否则您将看不到动画(所有函数都会延迟执行,但同时...在 1 秒后)

  3. 不要在每个函数调用上都创建一个新的折线,最后会有很多折线(每个项目 1 条)。创建一条折线并将位置推送到折线的路径

    $scope.autoRefresh = function() {
    try {
      var route = new google.maps.Polyline({
              path: [],
              strokeColor: '#FF0000',
              strokeOpacity: 2.0,
              strokeWeight: 3,
              editable: false,
              map:map
            }),
            index=0,
            marker=new google.maps.Marker({map:map,icon:icon});
    
      if (!_.isEmpty(items)) {
        angular.forEach(items, function(cordinates) {
          setTimeout(function ()
          {         
            route.getPath().push(new google.maps.LatLng(cordinates.lat, cordinates.lng));
    
            route.setMap(map);
            moveMarker(map, marker, cordinates.lat, cordinates.lng);
            markLAT = cordinates.lat;
            markLNG = cordinates.lng;
          }, 200*++index);
        });  
      }
      //  
    } catch (e) {
      console.log(e);
    }
    };
    

Plunker

【讨论】:

  • 嗨..我注意到你的回答中有一个问题......在预览期间在浏览器中打开另一个标签并花一些时间在它上面,.. ..一段时间后,如果您检查已生成的折线的预览,您会看到一些意外的折线与主折线一起绘制......我正在使用 chrome跨度>
  • 当您切换到另一个选项卡时,这可能是 chrome 中的超时管理不善(浏览器通常会尝试在非活动窗口中节省内存,例如通过延迟超时)。我现在得去上班了,待会我去看看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-05
相关资源
最近更新 更多