Google Map API Custom Marker Image

390 views Asked by At

I am looking for help to get a custom marker. The code is currently broken. Here is my script. When I put icon in the for loop, it breaks. When I remove it, everything works.

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 3,
  center: new google.maps.LatLng(35.239313, -41.073296),
  mapTypeId: google.maps.MapTypeId.HYBRID
});
map.setOptions({ minZoom: 3, maxZoom: 15 });
var infowindow = new google.maps.InfoWindow();
var image = 'images/research-pin.png';
var marker, i;

In this loop when I add "icon" to the for loop, it does not display. I need to display multiple markers all using the same custom image.

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,
    html: locations[0]
    icon: image
  });
  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i][0]);
      infowindow.open(map, marker);
    }
  })(marker, i));
}
1

There are 1 answers

0
geocodezip On

You are missing a comma. This:

marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map,
    html: locations[0]  //<----- missing comma
    icon: image
});

should be:

marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map,
    html: locations[0],  // <---- added comma
    icon: image
});

You should be getting a syntax error.