创建自定义GIcon的问题

时间:2011-07-09 00:41:01

标签: javascript google-maps google-maps-api-2

我曾经有类似的东西 -

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");`

工作正常 而不是那样,我想要像 -

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,category);
                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; 
                     }
                }`

它不起作用。不知道为什么。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

有一个拼写错误:

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

但是,我更喜欢使用1个对象而不是2个数组:

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

<小时/> 修改

请参阅GDownloadUrl("genxml.xml")

的回调函数

您可以在回调

内的链接文档的第224行创建标记
var marker = createMarker(point,name,html,category);

但是你稍后在回调函数(第257行)

中创建了GIcon
gicons[temp_tag] = new GIcon(baseIcon,tags_colors[i]);

结果是:在createMarker()内,gicons-object仍然为空,gicons[category]作为new GMarker()

的参数提供时未知

因此,GIcons的动态创建工作正常(您可以在firebugs DOM-tab中检查它),但它来得很晚。