开发者

Problem creating custom GIcon

I used to have something like this -

var gicons = [];

var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.iconAnchor = new GPoint(9,34);
baseIcon.iconSize = new GSize(20,34);
baseIcon.infoWindowAnchor = new GPoint(9,2);

gicons["home"] = new GIcon(baseIcon,"yellow.png"); 
gicons["red"] = new GIcon(baseIcon,"red.png");
gicons["green"] = new GIcon(baseIcon,"green.png");
gicons["blue"] = new GIcon(baseIcon,"blue.png");`

It works fine.

Instead of that, I would like something like -

var tags = ["home", "red","green", "blue"];
var tags_colors = ["yellow.png", "red.png", "green.png", "blue.png"];

for(var i=0; i<tags.length; i++){

   if(tags[i]!=null){ 
      gicons[tags[i]] = new GIcon(baseIcon,tags_colors[i]); 
   }
}

::::::EDIT::::

` GDownloadUrl("genxml.php", function(doc) { var xmlDoc = GXml.parse(doc); var markers = xmlDoc.documentElement.getElementsByTagName("marker");

            for (var i = 0; i < markers.length; i++) {
              // obtain the attribues of each marker
                var lat = parseFloat(markers[i].getAttribute("lat"));
                var lng = parseFloat(markers[i].getAttribute("lng"));
                var point = new GLatLng(lat,lng);
                var address = markers[i].getAttribute("area");
                var name = markers[i].getAttribute("street");
                var html = "<b>"+name+"<\/b><p>"+address;
                var category = markers[i].getAttribute("tag");
                // create the marker
                var marker = createMarker(point,name,html,cate开发者_Python百科gory);
                map.addOverlay(marker);
                tags[i] = markers[i].getAttribute('tag');

            }

            // hadnling the tags dynamically, to make it unique

           for (var i=0; i<tags.length-1 ; i++){
                var temp = tags[i];
                for(var j=i+1; j<tags.length; j++){
                     if(temp == tags[j]){
                        tags[j] = null; 
                     }
                }`

It is not working. No idea why. Can anyone help me please?


There is a typo:

var tag_colors =.....
    tags_colors[i]

However, i would prefer to use 1 object instead of 2 arrays:

var gicons={};
var tags = {'home'  :'yellow.png',
            'red'   :'red.png',
            'green' :'green.png',
            'blue'  :'blue.png'
           };

for(var k in tags){
  gicons[k] = new GIcon(baseIcon,tags[k]); 
}


EDIT:

See the callback-function for GDownloadUrl("genxml.xml")

You create the marker there in line 224 of the linked document, inside the callback

var marker = createMarker(point,name,html,category);

But you create the GIcon later inside the callback-function(line 257)

gicons[temp_tag] = new GIcon(baseIcon,tags_colors[i]);

The result is: inside createMarker() the gicons-object is still empty, gicons[category] is unknown when supplied as argument to new GMarker()

So the dynamic creation of the GIcons works fine(you can inspect it inside firebugs DOM-tab), but it comes to late.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜