简体   繁体   English

Google标记在地图上的动画加载超时

[英]Animation of google markers on map load with timeout

I would like to create a timeout on each google marker's DROP animation, but I think the closure code for the marker and the array item are conflicting. 我想在每个Google标记的DROP动画上创建一个超时,但是我认为标记和数组项的关闭代码相互冲突。 I do not know too much about closures and am getting a bit stuck on the problem. 我对闭包不了解太多,并且在这个问题上有点卡住了。

I can get them all falling at once. 我可以让他们一下子掉下来。

falling markers code jsfiddle 下降标记代码jsfiddle

but I would like to have a timeout after every marker of 100 ms. 但我希望每100 ms的标记后都有一个超时。

This is what I thought would work 我认为这是可行的

...

//Loop through nc array
for (var i = 0; i < nc.length; i++) {

   //Create 100 ms rule on marker creation
   setTimeout(function () {

     //Create marker
     var marker = new google.maps.Marker({
       position: nc[i],
       map: map,
       title: "tron" + i,
       animation: google.maps.Animation.DROP,
     });

    }, i * 100);

   //Creating the closure
   (function (i, marker) {

     //Add infowindow
     google.maps.event.addListener(marker, 'click', function () {
         if (!infowindow) {
            infowindow = new google.maps.InfoWindow();
         }
         //Setting content of info window
         infowindow.setContent('<h2>Tron lives | ' + i + '</h2>');
             infowindow.open(map, marker);
         });
      })(i, marker);
    };

...

But that does not work. 但这不起作用。 I figure that once the markers are created in the loop one would set the timeout on that creation process which would create the falling rain marker effect. 我认为,一旦在循环中创建了标记,就可以在该创建过程中设置超时,这将创建降雨标记效果。

Had the same thought as @Bryan Weaver. 与@Bryan Weaver有相同的想法。 Took your fiddle and modified it a bit to create the markers in a function, and iteratively set the timeout. 用了小提琴并对其进行了一些修改,以在函数中创建标记,并迭代设置超时。 Ended up with what follows, and it successfully does your "rain" effect. 最终得到以下结果,并且成功完成了“雨”效果。

    var addmarker = function(i) {
        //Create marker
            var marker = new google.maps.Marker({
                position: nc[i],
                map: map,
                title: "tron" + i,
                animation: google.maps.Animation.DROP,
            });

            //Creating the closure
            (function (i, marker) {   

                //Add infowindow
                google.maps.event.addListener(marker, 'click', function () {
                    if (!infowindow) {
                        infowindow = new google.maps.InfoWindow();
                    }
                    //Setting content of info window
                    infowindow.setContent('<h2>Tron lives | ' + i + '</h2>');

                    infowindow.open(map, marker);
                });
            })(i, marker); 
        if(i++ < nc.length) {
            setTimeout(function() {addmarker(i)}, 100);
        }

    }
    addmarker(0);             

Here's the full fiddle: http://jsfiddle.net/LzJ8B/ 这是完整的小提琴: http : //jsfiddle.net/LzJ8B/

Try this one: 试试这个:

var map;
var service;
var infowindow;
var mapcenter;
var markers = [];

  function setMapOnAll(map) {
    for (var i = 0; i < markers.length; i++) {
      markers[i].setMap(map);
    }
  }
  /*init map*/
  function initMap(){
     mapcenter= new google.maps.LatLng(-33.8617374,151.2021291);
     map = new google.maps.Map(document.getElementById('map'), {
     center: mapcenter,
     zoom: 10
   });

    infowindow = new google.maps.InfoWindow();
    service = new google.maps.places.PlacesService(map);
  };
  /*end init map*/

  function findPlaces(){
    service.nearbySearch({
      location: mapcenter,
      radius: 50000,
      type:['animal hospital'],
      keyword: ['pet animal emergency clinic hospital']
    }, nearbyCallback);
  }

  function nearbyCallback(results, status) {
    if (status === google.maps.places.PlacesServiceStatus.OK) {
      setMapOnAll(null); //clear markers

      for (var i = 0; i < results.length; i++) {
      /*important part of the code*/
          (function(results,i){
            if(results)
            setTimeout(function(){createMarker(results[i])},123*i);
          })(results,i)
      }
    }
  };

  function createMarker(place) {
    var placeLoc = place.geometry.location;
      var marker = new google.maps.Marker({
        map: map,
        position: place.geometry.location,
        animation:google.maps.Animation.DROP,
      });
        markers.push(marker);
        google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent('<div><h4 style="margin:0;padding:0">'+place.name+'</h4><p style="margin:0;padding:0;font-size:12px">'+place.vicinity+'</p></div>');
        infowindow.open(map, this);
      });
  };

//call findplaces document.ready event
$(function(){
    $('button').click(function(){findPlaces();});
   });
 });

In your HTML 在您的HTML中

<div id="map"  class="col12" style="width:100%;height:100%"></div>
<button>Load Places</button>
<script src="https://maps.googleapis.com/maps/api/js?key=--your-api-key--=places&callback=initMap" async defer></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM