animated gif in array in google map

277 views Asked by At

I am trying to use optimize:false parameter in my code to use animated gif as a mouseover:

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));

when I use for example:

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
    });

I have no problems. but when I try to use the same here:

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));

I get the image disappearing not animating Please suggest solution

based on the suggestions posted by the @geocoder here:

<!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>

as you can see the code in "Fir AZE" draws a layer. then later it(the drawn layer) is supposed to be shown on click , the example provided initially at this link www.visualguide.ca/example shows that. when I was asking about the solution for the animated marker I thought that I had problem with optimize :false - because all their lines of code were working ok. as I have tried to implement solution provided below it worked! but I lost my ability to show/hide layers on click.sorry if this was not clear initially

my intial code was:

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);

it was changed to

 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);
}

I have compared line by line to locate this, and am stumbled.

2

There are 2 answers

13
geocodezip On

One option is to create the marker with {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));
}

code snippet with animated 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>

0
Visual Guide On

somehow I have managed(with the help of geocoder and Alex - thanks

//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
});

I also managed to have my layers appearing on click