Fade In circles in Google Maps

南楼画角 提交于 2019-12-08 09:50:33

Fade the circles in (start at zero, end at one):

function polygon_fadein(polygon, seconds, callback){
    polygon.setOptions({fillOpacity:0, strokeOpacity:0});
    //  The "fade task" runs every 50 ms, seconds is the total time to fade,
    //   multiplied by approximately 1000 to turn it into milliseconds.
    var fill = 50/(seconds*999);
    var stroke = 50/(seconds*999);
    var fadein = setInterval(function(){
        if((polygon.get("strokeOpacity") > 0.99) &&  (polygon.get("fillOpacity") > 0.99)){
            clearInterval(fadein);
            if(typeof(callback) == 'function')
                callback();
            return;
        }
        polygon.setOptions({
            'fillOpacity': Math.min(1.0, polygon.fillOpacity+fill),
            'strokeOpacity': Math.min(1.0, polygon.strokeOpacity+stroke)
        });
    }, 50);
}

code snippet:

var nodeSelect = true;
// Boston 42.3584308, -71.0597732
var lat = [42.350542, 42.353036, 42.358249, 42.350101, 42.350190, 42.3634819];
var lon = [-71.074856, -71.059052, -71.057507, -71.087478, -71.059300, -71.0686226];
var nodeInfo = [];
var node = [];
var map = null;

function initialize() {
  for (var num = 0; num < lat.length; num++) {
    nodeInfo[num] = {
      center: new google.maps.LatLng(lat[num], lon[num])
    }
  }

  var styledMap = new google.maps.StyledMapType(styles, {
    name: "Styled Map"
  });

  // Options for map
  var mapOptions = {
    center: new google.maps.LatLng(42.35791, -71.063157),
    zoom: 15,
    mapTypeControlOptions: {
      mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
    }
  };

  map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

  map.mapTypes.set('map_style', styledMap);
  map.setMapTypeId('map_style');

  nodeDisplay();
}

function nodeDisplay() {
  var drawNode;

  for (var i in nodeInfo) {
    if (i >= 1) {
      if (nodeSelect == true) {
        drawNode = {
          strokeColor: '#0000FF',
          fillColor: '#FF0000', //'#0000FF',
          strokeWeight: 2,
          fillOpacity: 1,
          map: map,
          center: nodeInfo[i].center,
          radius: 40,
          visible: true
        };

      } else {
        drawNode = {
          strokeColor: '#0000FF',
          fillColor: '#FF0000', //'#0000FF',
          strokeWeight: 2,
          fillOpacity: 1,
          map: map,
          center: nodeInfo[i].center,
          radius: 40,
          visible: false
        };
      }
    } else {
      drawNode = {
        strokeColor: '#FF0000',
        fillColor: '#FF0000',
        strokeWeight: 2,
        fillOpacity: 1,
        map: map,
        center: nodeInfo[i].center,
        radius: 60
      };
    }

    node[i] = new google.maps.Circle(drawNode);
    polygon_fadein(node[i], 5);
    attachMessage(i);
  }
}

function clearOverlays() {
  for (var i in nodeInfo) {
    if (i > 0) {
      node[i].setMap(null);
    }
  }
}

function attachMessage(number) {
  var message = "Hello. This is node number " + number + ".";
  var infowindow = new google.maps.InfoWindow({
    size: new google.maps.Size(50, 50)
  });

  infowindow.setContent(message);

  google.maps.event.addListener(node[number], 'click', function() {
    infowindow.setPosition(node[number].getCenter());
    infowindow.open(map);
  });
}

function polygon_fadein(polygon, seconds, callback) {
  polygon.setOptions({
    fillOpacity: 0,
    strokeOpacity: 0
  });
  var fill = 50 / (seconds * 999);
  var stroke = 50 / (seconds * 999);
  var fadein = setInterval(function() {
    if ((polygon.get("strokeOpacity") > 0.99) && (polygon.get("fillOpacity") > 0.99)) {
      clearInterval(fadein);
      if (typeof(callback) == 'function')
        callback();
      return;
    }
    polygon.setOptions({
      'fillOpacity': Math.min(1.0, polygon.fillOpacity + fill),
      'strokeOpacity': Math.min(1.0, polygon.strokeOpacity + stroke)
    });
  }, 50);
}

google.maps.event.addDomListener(window, 'load', initialize);

// Styles
var styles = [{
  stylers: [{
    hue: "#2222EE"
  }, {
    saturation: -40
  }]
}, {
  featureType: "road",
  elementType: "geometry",
  stylers: [{
    lightness: 100
  }, {
    visibility: "simplified"
  }]
}, {
  featureType: "road",
  elementType: "labels",
  stylers: [{
    visibility: "off"
  }]
}, {
  featureType: "transit.station.bus",
  elementType: "labels.icon",
  stylers: [{
    visibility: "off"
  }]
}];
html,
body,
#map_canvas {
  margin: 0;
  padding: 0;
  height: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map-canvas" style="height: 600px; width:800px;"></div>
<div id="polygonCoords"></div>

(original code in the question most likely from: Is there a way to fade out a V3 google.maps.Polygon?)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!