【问题标题】:showing animation of poly-lines in googlemap based on user selection in drop-down根据下拉菜单中的用户选择在谷歌地图中显示折线动画
【发布时间】:2015-03-15 05:56:25
【问题描述】:

我已经创建了一个应用程序,用于在 google-maps 中为折线设置动画,一旦我们从下拉列表中选择特定用户,动画就会开始,该应用程序工作正常,但问题是,一旦特定动画如果我们选择另一个用户,应用程序将被挂起并显示意外动画,则会启动并继续进行,

谁能告诉我一些解决方案,一旦选择了另一个用户并需要在此之后显示所选用户的折线动画

Plunker

<html lang="en">

  <head>
    <script data-require="lodash.js@2.4.1" data-semver="2.4.1" src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://code.angularjs.org/1.2.16/angular.js"></script>
    <script type="text/javascript" src="script.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=panoramio"></script>
  </head>

  <body ng-app="app" ng-controller="Controller">

  <select ng-change="showGPSTracking()" ng-model="user">
    <option selected="selected" value="" class="">Select User</option>
    <option value="1458">1458</option>
    <option value="1658">1658</option>
  </select>
  </br></br></br></br>
    <div style="width: 880px;">
      <div id="map" style="width: 880px; height: 500px; float: left;"></div>
    </div>
  </body>

</html>

【问题讨论】:

  • 您的 Plunkr 网址现在对我来说似乎超时了......这可能是暂时的,但您最好将您的 javascript 添加到您的问题中
  • @duncan 我的 plunker 是否有任何问题,因为由于篇幅原因,我已将 js 包含在我的问题中......你可以在我的 plunker 中看到那里

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


【解决方案1】:

您遇到的问题是您有一个全局 map 变量。每次更改用户时,都会更新该映射并创建数百个函数调用。因此,第一张地图上的那些函数调用开始与第二张地图上的那些函数调用发生冲突——它们都在同一个地图对象上工作。

我在这里快速而肮脏的解决方法只是将地图对象设为局部变量,并将其传递给autoRefresh 函数。对第一张地图上的函数的调用仍然会发生,只是结果将不可见,因为当您将地图 div 重新分配给新地图时,您实际上已经隐藏了该地图。

我认为这并不理想,但它似乎对我有用。理想情况下,您可以通过某种方式“停止”其中一张地图上的动画,并阻止执行添加到路线的所有功能。

var app = angular.module('app', []);

app.controller("Controller", function($scope, $http, item) {
  $scope.tracker = {};
  var items;
  $scope.showLiveMap = false;
  $scope.showLiveMap = false;
  var firtslat, firstlong;

  var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png");
  var center = null;
  //var map = null;  - you don't need this any more
  var currentPopup;
  var bounds = new google.maps.LatLngBounds();
  var markLAT, markLNG, trackId;
  var marker;

  function moveMarker(map, marker, lat, lon) {
    try {
      marker.setPosition(new google.maps.LatLng(lat, lon));
      map.panTo(new google.maps.LatLng(lat, lon));

    } catch (e) {}
  }

  $scope.autoRefresh = function(map) {
    try {
      var route = new google.maps.Polyline({
              path: [],
              geodesic : true,
              strokeColor: '#FF0000',
              strokeOpacity: 1.0,
              strokeWeight: 2,
              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);  not necessary, you set the map when you created the route
            moveMarker(map, marker, cordinates.lat, cordinates.lng);
            markLAT = cordinates.lat;
            markLNG = cordinates.lng;
          }, 200*++index);
        });  
      }
      //  
    } catch (e) {
      console.log(e); 
    }
  }; 

function initMap(user)
{
  items = item.items[user];
  try {
    markLAT = items[items.length - 1].lat;
    markLNG = items[items.length - 1].lng;
    var map = new google.maps.Map(document.getElementById("map"), {
      center: new google.maps.LatLng(markLAT, markLNG),
      zoom: 10,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      mapTypeControlOptions: {
        style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
      }, 
      navigationControl: true,
      navigationControlOptions: {
        style: google.maps.NavigationControlStyle.SMALL
      }
    });
    $scope.autoRefresh(map); // passed the local map variable into the function
  } catch (e) {
    console.log(e);
  }
}

  $scope.showGPSTracking = function() {
    if (!_.isNull($scope.user)) {
      initMap($scope.user);
    } else {
      console.log("entered");
      $scope.showLiveMap = false;
    }
  };
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    相关资源
    最近更新 更多