custom-marker-info-window

Google map custom marker with info window on click

by Vishnuprasad ps

HTML

<div id="map"></div>

<script>
  var map;

  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      zoom: 17,
      center: new google.maps.LatLng(40.712696, -74.005019),
      mapTypeId: 'roadmap'
    });

    var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
    var icons = {
      parking: {
        icon: iconBase + 'parking_lot_maps.png'
      },
      library: {
        icon: iconBase + 'library_maps.png'
      },
      info: {
        icon: iconBase + 'info-i_maps.png'
      }
    };

    var features = [{
      position: new google.maps.LatLng(40.712696, -74.005019),
      content: 'test content one',
      type: 'parking'
    }, {
      position: new google.maps.LatLng(40.712753, -74.006081),
      content: 'test content two',
      type: 'parking'
    }, {
      position: new google.maps.LatLng(40.713664, -74.007819),
      content: 'test content three <a href="http://www.google.com">A link to google</a>',
      type: 'library'
    }];

    var infowindow = new google.maps.InfoWindow({
      content: ""
    });

    // Create markers.
    features.forEach(function(feature) {
      var marker = new google.maps.Marker({
        position: feature.position,
        content: feature.content,
        icon: icons[feature.type].icon,
        map: map
      });
      var content = "<a href='" + feature.content + "'>A link to google</a>";
      marker.addListener('click', function() {
        infowindow.setContent('<div><strong>' + marker.content + '</strong></div>');

        infowindow.open(map, marker);
      });
    });





  }

</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDvkk7wNQcIYXZ7S8XNG8cG-elq0QE2v3k&callback=initMap">


</script>

CSS

html,
  body {
    height: 100%;
    margin: 0;
    padding: 0;
  }
  
  #map {
    height: 100%;
  }