如何在Google地图上一一加载标记动画

时间:2019-06-29 12:33:46

标签: php ajax laravel google-maps google-maps-markers

我在地图上有一些带有Ajax的Google标记代码。它的工作很好。现在我要一个一个地放下标记,而不是一次全部加载。请帮助我

$.ajax({
            type: 'get',
            url: APP_URL + '/yestoday',
            data: {_token:"{{csrf_token()}}"},
            success: function (data) {
              debugger;
               // console.log(data);
                var locations  = Array();
                var map = new google.maps.Map(document.getElementById('map'), {
                center: new google.maps.LatLng(20.593683,78.962883),
                zoom: 7,
                });
                jQuery.each(data , function (index, value){

                var points = Array();
                var point = new google.maps.LatLng(parseFloat(value.latitude),parseFloat(value.longitude));

                points.push(parseFloat(value.latitude));
                points.push(parseFloat(value.longitude));
                points.push(value.store_name);
                points.push(value.store_address);  

                locations.push(points);

                var marker = new google.maps.Marker({
                    position: point,
                    map: map,
                    animation: google.maps.Animation.DROP,
                });

                var infowindow = new google.maps.InfoWindow();
                google.maps.event.addListener(marker, 'mouseover', function() {
                  infowindow.setContent('<div><strong>'+points[2]+'</strong><br><strong>'+points[3]+'</strong></div>');
                  infowindow.open(map, this);
                });
                google.maps.event.addListener(marker, 'mouseout', function() {
                  infowindow.close();
                });
            });
        },
    });

2 个答案:

答案 0 :(得分:1)

您可以使用setTimeout函数

  

set delayMarker = 200; //您可以在此处设置延迟时间

$.ajax({
    type: 'get',
    url: APP_URL + '/yestoday',
    data: {_token:"{{csrf_token()}}"},
    success: function (data) {
        var locations  = Array();
        var map = new google.maps.Map(document.getElementById('map'), {
            center: new google.maps.LatLng(20.593683,78.962883),
            zoom: 7,
        });

        var delayMarker = 200;
        jQuery.each(data , function (index, value){

            setTimeout(function() {

                var points = Array();
                var point = new google.maps.LatLng(parseFloat(value.latitude),parseFloat(value.longitude));

                points.push(parseFloat(value.latitude));
                points.push(parseFloat(value.longitude));
                points.push(value.store_name);
                points.push(value.store_address);
                locations.push(points);

                var marker = new google.maps.Marker({
                    position: point,
                    map: map,
                    animation: google.maps.Animation.DROP,
                });

                var infowindow = new google.maps.InfoWindow();
                google.maps.event.addListener(marker, 'mouseover', function() {
                    infowindow.setContent('<div><strong>'+points[2]+'</strong><br><strong>'+points[3]+'</strong></div>');
                    infowindow.open(map, this);
                });

                google.maps.event.addListener(marker, 'mouseout', function() {
                    infowindow.close();
                });

            },index * delayMarker);

        });
    },
});

答案 1 :(得分:0)

您可以使用 setTimeout();

您可以在Google Maps API文档中找到一个示例,该示例可以完成您想要的操作: https://developers.google.com/maps/documentation/javascript/examples/marker-animations-iteration#try-it-yourself

function drop() {
  clearMarkers();
     for (var i = 0; i < neighborhoods.length; i++) {
         addMarkerWithTimeout(neighborhoods[i], i * 200); // Increase this value to slower the animation or decrease it to make it faster
     }
}

function addMarkerWithTimeout(position, timeout) {
  window.setTimeout(function() {
     markers.push(new google.maps.Marker({
         position: position,
         map: map,
         animation: google.maps.Animation.DROP
     }));
         }, timeout);
}

function clearMarkers() {
   for (var i = 0; i < markers.length; i++) {
      markers[i].setMap(null);
   }
      markers = [];
}