简体   繁体   中英

Google Map markers - add a custom icon with a unique label for each marker

I have implemented a google map with multiple markers but what i am failing to do is how to add a unique label for each marker ie Each marker needs have a letter:
eg
Marker 1 needs to display 'A'
Marker 2 needs to display 'B'
Marker 3 needs to display 'C'
Marker 4 needs to display 'D'
...
an example of what i am trying to achieve is: http://www.athenos.com/locator/ --- enter 11205 in the zip search

here is a portion of my map code - my init and add_marker methods:

init : function() {

    var self = this;

    // set map property
    var map = new google.maps.Map(self.dom_element, self.options);
    self.map = map;

    // set some other shit
    new google.maps.Size(95, 77),
    new google.maps.Point(0,0),
    new google.maps.Point(47, 76);

    // creating new bounds 
    var bounds = new google.maps.LatLngBounds();

    // for loop to iterate through locations
    for (var i = 0; i < self.locations.length; i++) {

        // extend the bounds
        bounds.extend(self.locations[i].latlng);

        // add the marker
        self.add_marker(self.locations[i].latlng, i);
    }

    // centers the map based on the existing map markers
    self.map.fitBounds(bounds);
},

add_marker : function(latlng, marker_index) {
    var self = this;
    var marker = new google.maps.Marker({
        position: latlng,
        map: self.map,
        icon: self.map_icon,
        zIndex: 998,
        id: marker_index // give the marker an ID e.g. 0, 1, 2, 3 - use this for indexing the listed venues
    });

    google.maps.event.addListener(marker, 'mouseover', function(event) {

        var this_marker = this;
        // executes handler and passes the marker as 'this' and event data as an argument
        self.handle_marker_mouseover.call(self, this, event);
        this_marker.setZIndex(999);

    });

    google.maps.event.addListener(marker, 'mouseout', function(event) {

        // executes handler and passes the marker as 'this' and event data as an argument
        self.handle_marker_mouseout.call(self, this, event);


    });

    google.maps.event.addListener(marker, 'click', function(event) {

        // executes handler and passes the marker as 'this' and event data as an argument
        self.handle_marker_click.call(self, this, event);


    });
},

...

Please help. Thanks in advance

just try to locate your icon, in marker's prop, at this url: http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=7|00FF00|000000

So iterate with letters over the first part of the chld querystring parameter, and don't forget to choose your marker's color (latest two part, pipe separated)

Take a look at the article below. It's very simple. Use a marker with numeric labels or a marker with alphabet label (A,B..)

Multiple marker with labels in google map

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);
}

Go to this article for more details

Multiple marker with labels in google map

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM