Randy
Randy

Reputation: 31

define size and anchor for multiple markers in google maps api v3

There are several similar questions and answers, but I do can't figure it really out! So, I use two different icons (customIcons), but they are placed slightly incorrect on the map due to the default setting "the anchor is located along the center point of the bottom of the image." (see https://developers.google.com/maps/documentation/javascript/reference#MarkerOptions). I know that I have to change that default feature, but do not know how. I define the custom icons by

var markers = [];
var infoWindow;
var customIcons = {
recent: {icon: 'http://www.herpetofauna-nrw.de/Fundmeldungen/blauer_Punkt.png'},
historisch: {icon: 'http://www.herpetofauna-nrw.de/Fundmeldungen/roter_Punkt.png'}
};

and later i use:

downloadUrl("auswertung.xml?ienocache="+new Date().getMilliseconds(), function(data){ 
       var xml = data.responseXML; 
      var markers = xml.documentElement.getElementsByTagName("marker");
      var bounds = new google.maps.LatLngBounds();
      for (var i = 0; i < markers.length; i++) {
        var Art = markers[i].getAttribute("Art");
        var type = markers[i].getAttribute("Stichjahr");
        var Jahr = markers[i].getAttribute("Jahr");
        var MTBNr = markers[i].getAttribute("MTBNr");
        var latlng = new google.maps.LatLng(
            parseFloat(markers[i].getAttribute("lat")),
            parseFloat(markers[i].getAttribute("lng")));

       var html = 
         "<b>" + "Art: "  + "</b>" + Art +  " <br/>" +
       "<b>" + "MTB: " +  "</b>" + MTBNr + "<br/>" +
       "<b>" + "Funddatum: " +  "</b>" + Jahr + "<br/>"; 
       var icon = customIcons[type] || {};

       var marker = new google.maps.Marker({
        map: map,
        position: latlng,
        icon: icon.icon
        });     

But as I use "var icon = customIcons[type] || {};" I don't know where to place the necessary option "new google.maps.Point(0,0)" like:

var image = new google.maps.MarkerImage('XXX.png', // The image size
    new google.maps.Size(44, 46), // The origin
    new google.maps.Point(0,0),   // The anchor
    new google.maps.Point(22, 23));

Could anybody please give help? Many thanks in advance!

Upvotes: 0

Views: 3685

Answers (1)

geocodezip
geocodezip

Reputation: 161384

MarkerImage is deprecated in favor of the Icon anonymous object. Something like this should work (not tested):

var customIcons = {
recent: {icon: {url:'http://www.herpetofauna-nrw.de/Fundmeldungen/blauer_Punkt.png',
                anchor: new google.maps.Point(X1,Y1),
                size: new google.maps.Size(S1,S2)}},
historisch: {icon: {url:'http://www.herpetofauna-nrw.de/Fundmeldungen/roter_Punkt.png',
                    anchor: new google.maps.Point(X2, Y2),
                    size: new google.maps.Size(S2,S2)}}
};

Upvotes: 2

Related Questions