How to add a marker with a different icon on the google map?

I have this code:

<div id="map_canvas"></div>
<script>
   window.onload = function () {
       var styles = [{"featureType":"all"}];
       var bounds = new google.maps.LatLngBounds();
       var options = {
           mapTypeControlOptions: {
               mapTypeIds: ['Styled']
           },
           center: new google.maps.LatLng(11, 22),
           zoom: 15,
           disableDefaultUI: true,
           mapTypeId: 'Styled'
       };
       marker = new google.maps.Marker({
           map:map,
       });
       var div = document.getElementById('map_canvas');
       var map = new google.maps.Map(div, options);
       var styledMapType = new google.maps.StyledMapType(styles, { name: 'Styled' });

       var markers = [
                        [
                          'Atrakcja 1 pl', 
                           51.73925413, 
                           19.51309225, 
                           'Atrakcja 1 pl', 
                           '#'
                         ],
                         [
                           'Atrakcja 2 PL', 
                            53.41475000, 
                            14.60220358, 
                           'Atrakcja 2 PL', 
                           '#'
                         ],
                     ];
       var infoWindow= new google.maps.InfoWindow({maxWidth:600}),
           marker, i,
           image = 'http://localhost/assets/poi.png';

       for( i = 0; i < markers.length; i++ ) {
           var beach = markers[i];
           var position = new google.maps.LatLng(beach[1], beach[2]);
           bounds.extend(position);
           marker = new google.maps.Marker({
               position: position,
               map: map,
               icon: image,
               title: beach[0],
               myurl: beach[4]
           });

           google.maps.event.addListener(marker, 'click', (function(marker, i) {
               return function() {
                   infoWindow.setContent('<a href="'+marker['myurl']+'">'+marker['title']+'</a>');
                   infoWindow.open(map, marker);
               }
           })(marker, i));
           map.fitBounds(bounds);
           map.mapTypes.set('Styled', styledMapType);

       }
   }
</script>

The code creates a google map with many markers. In the above example there are 2 points. Points are centered on the map. I would like to add one more point:

['Main Point', 51.424150124, 14.6032790-8, 'Main Point', '#']

about the marker:

image2 = 'http://localhost/assets/poi2.png';

How can I do this in my code? I would like to have 2 types of markers on the map (2 colors).

1 answer

  • answered 2018-11-08 09:48 duncan

    Can you just add the image name to your markers array, for each point?

    var markers = [
        ['Atrakcja 1 pl', 51.73925413, 19.51309225, 'Atrakcja 1 pl', '#', 'poi.png'],
        ['Atrakcja 2 PL', 53.41475000, 14.60220358, 'Atrakcja 2 PL', '#', 'poi.png'],
        ['Main Point', 51.424150124, 14.6032790-8, 'Main Point', '#', 'poi2.png']
    ];
    
    var infoWindow= new google.maps.InfoWindow({maxWidth:600}),
    marker, i;
    
    for( i = 0; i < markers.length; i++ ) {
        var beach = markers[i];
        var position = new google.maps.LatLng(beach[1], beach[2]);
        bounds.extend(position);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            icon: 'http://localhost/assets/' + beach[5],
            title: beach[0],
            myurl: beach[4]
        });
    
        ...
    }