Marker Labels

by Marcel Ferdinand

HTML

<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>

CSS

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

JavaScript

// In the following example, markers appear when the user clicks on the map.
// Each marker is labeled with a single alphabetical character.
var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var labelIndex = 0;

function initialize() {
 var centerMap = { lat: 13.00, lng: 77.59 };
  var bangalore = { lat: 12.97, lng: 77.59 };
  
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 12,
    center: centerMap
  });
  
  var image = 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png';
  
  var beachMarker = new google.maps.Marker({
    position: { lat: 13.02, lng: 77.59 },
    map: map,
    icon: image
  });

  // This event listener calls addMarker() when the map is clicked.
//  google.maps.event.addListener(map, 'click', function(event) {
//    addMarker(event.latLng, map);
//  });

  // Add a marker at the center of the map.
  addMarker(bangalore, map);
}

// Adds a marker to the map.
function addMarker(location, map) {
  // Add the marker at the clicked location, and add the next-available label
  // from the array of alphabetical characters.
  var marker = new google.maps.Marker({
    position: location,
    label: labels[labelIndex++ % labels.length],
    map: map
  });
}

google.maps.event.addDomListener(window, 'load', initialize);