Skip to main content

Multiple marker with labels in google map

In this article you will see how to add multiple marker with label in google map (marker with label text), you can write your custom marker label text or use inbuilt A-Z marker icon , custom marker css and info window that will display on clicking marker.

Below is the two method that will add multiple marker in google map.

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
   //Generate Markers Value Array
    var markers = [
    <asp:Repeater ID="rptMarkers" runat="server">
    <ItemTemplate>
                {
                "title":'<%# Eval("Area") %>',
                "lat": '<%# Eval("Latitute") %>',
                "lng": '<%# Eval("Longitute") %>',
                "description": '<%# Eval("Address") %>'
            }
    </ItemTemplate>
    <SeparatorTemplate>
        ,
    </SeparatorTemplate>
    </asp:Repeater>
    ];

</script>

Script render like this :

<script type="text/javascript">
//Generate Markers Value Array
var markers = [
    {
        "title": 'Gombak Setia',
        "lat": '3.218937',
        "lng": '101.7167931',
        "description": 'Gombak Setia, Kuala Lumpur, Wilayah Persekutuan Kuala Lumpur, 53100'
    },
    {
        "title": 'Jalan Desa Melawati',
        "lat": '3.2331742',
        "lng": '101.7061447',
        "description": 'Jalan Desa Melawati, Kuala Lumpur, Wilayah Persekutuan Kuala Lumpur, 53100'
    },
    {
        "title": 'Jalan Intan (Gombak)',
        "lat": '3.22317',
        "lng": '101.71588',
        "description": 'Jalan Intan (Gombak), Kuala Lumpur, Wilayah Persekutuan Kuala Lumpur, 53100'
    },
    {
        "title": 'Jalan Madrasah',
        "lat": '3.218515',
        "lng": '101.717801',
        "description": 'Jalan Madrasah, Kuala Lumpur, Wilayah Persekutuan Kuala Lumpur, 53100'
    }
];
</script>


First Way MapWithMarker JS:

Use mapwithmarker.js script that will be use for writing label text upon marker icon, and css that is for how label text appears upon the marker icon, this will write numaric label value upon marker icon.
DownLoad MapWithMarker.JS Click Here
<style type="text/css">
   .labels { color: black; background-color: #FF8075; font-family: Arial; font-size: 11px; font-weight: bold; text-align: center; width: 12px; }
</style>
<script src="mapwithmarker.js" type="text/javascript"></script>

<script type="text/javascript">
  window.onload = function () {
    var mapOptions = {
        center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
        zoom: 12,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);

    var infoWindow = new google.maps.InfoWindow();
   
    for (i = 1; i <= markers.length; i++) {
        var data = markers[i-1]
        var myLatlng = new google.maps.LatLng(data.lat, data.lng);
       
        var marker = new MarkerWithLabel({
            position: myLatlng,
            map: map,
            title: data.title,
            labelContent: i,
            labelAnchor: new google.maps.Point(7, 30),
            labelClass: "labels", // the CSS class for the label
            labelInBackground: false
         });

        (function (marker, data) {
            google.maps.event.addListener(marker, "click", function (e) {
                infoWindow.setContent(data.description);
                infoWindow.open(map, marker);
            });
        })(marker, data);

    }
  }

</script>

//HTML: Google map will render inside this div

<div id="dvMap" style="width: 800px; height: 700px;">
</div>
Multiple marker with numaric labels text in google map

Second Way Marker Icon :

If you have only less than 26 markers then you can use this easy method marker icon which is provided by google only A-Z icon is available.

Eg:-
http://maps.google.com/mapfiles/markerA.png
http://maps.google.com/mapfiles/markerB.png
http://maps.google.com/mapfiles/markerC.png

<script type="text/javascript">
  window.onload = function () {
    var mapOptions = {
        center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
        zoom: 12,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);

    var infoWindow = new google.maps.InfoWindow();

    for (i = 1; i <= markers.length; i++) {
        var letter = String.fromCharCode("A".charCodeAt(0) + i - 1);
        var data = markers[i - 1]
        var myLatlng = new google.maps.LatLng(data.lat, data.lng);

        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: data.title,
            icon: "http://maps.google.com/mapfiles/marker" + letter + ".png"
        });

        (function (marker, data) {
            google.maps.event.addListener(marker, "click", function (e) {
                infoWindow.setContent(data.description);
                infoWindow.open(map, marker);
            });
        })(marker, data);
    }
 }

</script>

//HTML: Google map will render inside this div

<div id="dvMap" style="width: 800px; height: 700px;">
</div>
Multiple marker with alphabet labels text in google map

Popular posts from this blog

regex - check if a string contains only alphabets c#

How to validate that input string contains only alphabets, validating that textbox contains only alphabets (letter), so here is some of the ways for doing such task. char have a property named isLetter which is for checking if character is a letter or not, or you can check by the regular expression  or you can validate your textbox through regular expression validator in asp.net. Following code demonstrating the various ways of implementation.

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

How to validate dropdownlist in JavaScript

In this article you will see how to put validation in dropdownlist by javascript, suppose first item value of dropdownlist is 0 and text is "-Select-" just like given below and we have to validate that at least one item is selected excluding default i.e "-Select-".

Regular expression for alphanumeric with space in asp.net c#

How to validate that string contains only alphanumeric value with some spacial character and with whitespace and how to validate that user can only input alphanumeric with given special character or space in a textbox (like name fields or remarks fields). In remarks fields we don't want that user can enter anything, user can only able to enter alphanumeric with white space and some spacial character like -,. etc if you allow. Some of regular expression given below for validating alphanumeric value only, alphanumeric with whitspace only and alphanumeric with whitespace and some special characters.