谷歌地图中数组中的动画gif

animated gif in array in google map

本文关键字:gif 动画 数组 谷歌地图      更新时间:2023-09-26

我正试图在代码中使用optimize:false参数,将动画gif用作鼠标悬停:

var icon1 = "circle.png";
var icon2 = "circlered.gif";

var markers = [];
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
  map: map,
  visible: false,
  icon: icon1
});
google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
  return function(evt) {
    infowindow.setContent(locations[i][0] + "<br>" + marker.getPosition().toUrlValue(6));
    infowindow.open(map, marker);
    marker.setIcon(icon2);
  }
})(marker, i));
markers.push(marker); // save all markers
google.maps.event.addListener(marker, 'mouseout', (function(marker) {
  return function(evt) {
    infowindow.close();
    marker.setIcon(icon1);
  }
})(marker));

当我使用例如:

for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map,
    visible: false,
    icon: icon2, //here I purposely changed it to animated gif first
    optimize: false
    });

我没有问题。但当我尝试在这里使用相同的:

google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
    return function(evt) {
    infowindow.setContent(locations[i][0] + "<br>" + marker.getPosition().toUrlValue(6));
    infowindow.open(map, marker);
    marker.setIcon(icon2);
optimize:false;// here is the wrong code obviously 
  }
})(marker, i));

我得到的图像消失没有动画请建议解决方案

基于@geocoder在此处发布的建议:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
 <style>
html, 
body,
#map-canvas{
    width: 100%; 
    margin: 0px; 
    padding: 0px; 
    height: 880px;
    z-index:2
    }
#maptwo {
    z-index:1
}
    </style>    
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<!-- Include jQuery -->
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
var map;
var infowindow = new google.maps.InfoWindow();
function initialize() {
var map = new google.maps.Map(
document.getElementById("map-canvas"), {
  center: new google.maps.LatLng(40.222869, 47.602673),
  zoom: 13,
  mapTypeId: google.maps.MapTypeId.TERRAIN
});
var icon1 = "http://maps.google.com/mapfiles/ms/micons/blue.png";
var icon2 = {
url: "http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-35/e8nZC.gif",
scaledSize: new google.maps.Size(75, 100)
};

var markers = [];
var marker, i;
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
  position: new google.maps.LatLng(locations[i][1], locations[i][2]),
  map: map,
  optimized: false,
  visible: false,
  icon: icon1
});
bounds.extend(marker.getPosition())
google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
  return function(evt) {
    infowindow.setContent(locations[i][0] + "<br>" + marker.getPosition().toUrlValue(6));
    infowindow.open(map, marker);
    marker.setIcon(icon2);
  }
})(marker, i));
markers.push(marker); // save all markers
google.maps.event.addListener(marker, 'mouseout', (function(marker) {
  return function(evt) {
    infowindow.close();
    marker.setIcon(icon1);
  }
})(marker));
/* Change markers on zoom */
google.maps.event.addListener(map, 'zoom_changed', function() {
var zoom = map.getZoom();
// iterate over markers and call setVisible
for (i = 0; i < locations.length; i++) {
    markers[i].setVisible(zoom >= 11);
}
});     
  }
  map.fitBounds(bounds);
  }
google.maps.event.addDomListener(window, "load", initialize);
var locations = [
['Location1', 39.031586, 46.590031, 5],
['Location2', 38.998439, 46.557591, 4],
['Location3', 38.913429, 46.547370, 3],
['Location4', 39.090245, 46.703794, 2],
['Location5', 39.130588, 46.696239, 1]
  ];
//here I create a function that will show/hide layers that are defined by the  latLng  values  
function toggleLayer(firLayer, id) {
  if ($('#' + id).is(':checked')) {
firLayer.setMap(map);
  } else {
firLayer.setMap(null);
  }
}
//this is the end of the function
// Fir AZE is one of the many layers that are drawn
        drawAZE = new google.maps.Polygon({
            path: firAZE,
            strokeColor: '#000000',
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: '#00CCFF',
            fillOpacity: 0.15
        }); 
//end of FirAZE  end of layer
// Add a listener for the drawAZE mouseover/mouseout event.
google.maps.event.addListener(drawAZE ,'mouseover',function(){
this.setOptions({fillColor: "#FF0000"},{fillOpacity:"0.8"});
}); 
google.maps.event.addListener(drawAZE ,'mouseout',function(){
this.setOptions({fillColor: "#00CCFF"},{fillOpacity:"0.5"});
});
//end of drawAZE listener

</script>
</head>
<body>
<div id="map-canvas"></div>
<div id="maptwo"></div>
<input id="fir_azerbaijan" type="checkbox" onClick="toggleLayer(drawAZE,'fir_azerbaijan')" /> AZERBAIJAN
</body>
</html>

如您所见,代码在"Fir AZE"中绘制了一个图层。然后稍后它(绘制的层)应该在点击时显示,最初在该链接www.visualguide.ca/example上提供的示例显示了这一点。当我询问动画标记的解决方案时,我认为我在优化方面有问题:false,因为他们所有的代码行都正常工作。当我尝试实现下面提供的解决方案时,它成功了!但我失去了在点击时显示/隐藏图层的能力。如果最初不清楚的话,很抱歉

我最初的代码是:

function initialize() {
 var mapOptions = {
 zoom: 8,
 center: new google.maps.LatLng(40.222869, 47.602673),
 mapTypeId: google.maps.MapTypeId.TERRAIN,
 zIndex: 3
  };
// Set map    
 map = new google.maps.Map(document.getElementById('map-canvas'),
  mapOptions);

它被改为

 function initialize() {
 var map = new google.maps.Map(
 document.getElementById("map-canvas"), {
   center: new google.maps.LatLng(40.222869, 47.602673),
   zoom: 13,
   mapTypeId: google.maps.MapTypeId.TERRAIN
 });
var bounds = new google.maps.LatLngBounds();
  for (i = 0; i < locations.length; i++) {
   marker = new google.maps.Marker({
  position: new google.maps.LatLng(locations[i][1], locations[i][2]),
  map: map,
  optimized: false,
  visible: false,
  icon: icon1
});
bounds.extend(marker.getPosition())
//....
}
map.fitBounds(bounds);
}

我已经一行一行地比较了这个位置,结果被绊倒了。

一个选项是使用{optimized: false}:创建标记

for (i = 0; i < locations.length; i++) {
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map,
    optimized: false,
    icon: icon1
  });
  google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
    return function(evt) {
      infowindow.setContent(locations[i][0] + "<br>" + marker.getPosition().toUrlValue(6));
      infowindow.open(map, marker);
      marker.setIcon(icon2);
    }
  })(marker, i));
  markers.push(marker); // save all markers
  google.maps.event.addListener(marker, 'mouseout', (function(marker) {
    return function(evt) {
      infowindow.close();
      marker.setIcon(icon1);
    }
  })(marker));
}

带有动画gif的代码片段:

var map;
var infowindow = new google.maps.InfoWindow();
function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var icon1 = "http://maps.google.com/mapfiles/ms/micons/blue.png";
  var icon2 = {
    url: "http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-35/e8nZC.gif",
    scaledSize: new google.maps.Size(75, 100)
  };
  var markers = [];
  var marker, i;
  var bounds = new google.maps.LatLngBounds();
  for (i = 0; i < locations.length; i++) {
    marker = new google.maps.Marker({
      position: new google.maps.LatLng(locations[i][1], locations[i][2]),
      map: map,
      optimized: false,
      icon: icon1
    });
    bounds.extend(marker.getPosition())
    google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
      return function(evt) {
        infowindow.setContent(locations[i][0] + "<br>" + marker.getPosition().toUrlValue(6));
        infowindow.open(map, marker);
        marker.setIcon(icon2);
      }
    })(marker, i));
    markers.push(marker); // save all markers
    google.maps.event.addListener(marker, 'mouseout', (function(marker) {
      return function(evt) {
        infowindow.close();
        marker.setIcon(icon1);
      }
    })(marker));
  }
  map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, "load", initialize);
var locations = [
  ['palace', 52.231871, 21.005841],
  ['arkadia', 52.257305, 20.984481],
  ['stadium', 52.215147, 21.035074]
];
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

不知怎么的,我已经管理好了(在geocoder和Alex的帮助下-感谢

//announce my variables
var icon1 = "circle.png";
var icon2 = "circlered.gif";

var markers = [];
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
  position: new google.maps.LatLng(locations[i][1], locations[i][2]),
  map: map,
  optimized:false, // <-- required for animated gif
  visible: false,
  icon: icon1
});

我还设法让我的图层出现在点击时