Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom google map marker / pin (color)

I've done pretty decent getting some google map customizations; but I'm wondering where / or what I can add below to simply change the map marker / pin / or icon. I am really seeking to just change the color; but will create an image and do it that way if I must.

Below is what I am working with; cheers/

window.onload = function() {  

function initialize() {
    var stylez = [
      {
        featureType: "all",
        stylers: [
          { hue: "#c3c367" },
          { saturation: -75 }
        ]
      },
      {
        featureType: "poi",
        elementType: "label",
        stylers: [
          { visibility: "off" }
        ]
      }
    ];

    var latlng = new google.maps.LatLng(34.101958, -118.327925), // toggle per data

        mapOptions = {
            mapTypeControlOptions: {
                mapTypeIds: [google.maps.MapTypeId.ROADMAP, "Edited"] 
            },
            zoom: 14,
            center: latlng
        },

        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions),

        styledMapType = new google.maps.StyledMapType(stylez, {name: "Edited"}),

        marker = new google.maps.Marker({
            position: latlng, 
            map: map, 
            animation: google.maps.Animation.DROP,
            title:"Hello World!"
        }),

        infowindow = new google.maps.InfoWindow({
            content: "<div><img width='50' height='50' src='assets/icos/homico.png'</div>"
        });

        map.mapTypes.set("Edited", styledMapType);
        map.setMapTypeId('Edited');

    function toggleBounce () {
        if (marker.getAnimation() != null) {
            marker.setAnimation(null);
        } else {
            marker.setAnimation(google.maps.Animation.BOUNCE);
        }
    }

    // Add click listener to toggle bounce
    google.maps.event.addListener(marker, 'click', function () {
        toggleBounce();
        infowindow.open(map, marker);
        setTimeout(toggleBounce, 1500);
    });
}

// Call initialize -- in prod, add this to window.onload or some other DOM ready alternative
initialize();

};
like image 701
fred randall Avatar asked Apr 25 '26 22:04

fred randall


2 Answers

You simply need to add

"icon": "url"

to your marker declaration. So This:

marker = new google.maps.Marker({
        position: latlng, 
        map: map, 
        animation: google.maps.Animation.DROP,
        title:"Hello World!"
    })

Becomes:

marker = new google.maps.Marker({
        position: latlng, 
        map: map,
        icon: yourIconUrl,
        animation: google.maps.Animation.DROP,
        title:"Hello World!"
    })
like image 67
ChrisSwires Avatar answered Apr 30 '26 14:04

ChrisSwires


I already answered a similar question here, does it solve your problem ?

You can customize your own marker using this URL :

http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=D|00FF00|000000

chld parameter is the letter you want to appear in your marker.
After the pipe, the first RGB code if the color of the marker, the second is the background color of the marker. The last one is optional.

So you can create your marker like this :

var myPin= new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=D|00FF00|000000");

and use it like you want !

like image 40
AlexB Avatar answered Apr 30 '26 14:04

AlexB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!