2011-01-27 54 views
0

我有一個函數可以循環使用標記數據的XML文件。我需要設置我的地圖,這樣在循環中的最後一個標記被放大。谷歌地圖V3 - 循環標記並將地圖縮放到最後一個標記

var image = 'images/Icon.png' 
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    downloadUrl("phpsqlajax_genxml4.php", function(data) { 
     var markers = data.documentElement.getElementsByTagName("marker"); 
     for (var i = 0; i < markers.length; i++) { 
     var latlng = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")), 
            parseFloat(markers[i].getAttribute("lng"))); 
     var marker = createMarker(markers[i].getAttribute("name"), markers[i].getAttribute("ft"), markers[i].getAttribute("inch"), markers[i].getAttribute("lbs"), markers[i].getAttribute("oz"), markers[i].getAttribute("date"), markers[i].getAttribute("pic"), latlng); 


     } 
    }); 
    function createMarker(name, ft, inch, lbs, oz, date, pic, latlng) { 
    var marker = new google.maps.Marker({position: latlng, map: map, icon: image}); 

    var html = "<table border='0'>" + 
    "<tr>" + 
    "<td width='150'>" + "<strong>" + name + "<br>" + ft + " " + inch + " &nbsp;&nbsp;&nbsp;" + lbs + " " + oz + "</strong><br>" + date + "</td>" + 
    "<td width='100'>" + '<img src="image.jpg?width=80&amp;height=80&amp;cropratio=1:1&amp;image=' + pic +'"/>' + "</td> </tr> </table>"; 
    google.maps.event.addListener(marker, "click", function() { 
     if (infowindow) infowindow.close(); 
     infowindow = new google.maps.InfoWindow({content: html}); 
     infowindow.open(map, marker); 
    }); 
    return marker; 
    } 

回答

1

嘗試使用此居中標誌

 map.setCenter(new GLatLng(parseFloat(markers[0].getAttribute("lat")), 
parseFloat(markers[0].getAttribute("lng"))),3); 
+0

嗯,我不斷收到錯誤「的GLatLng不確定」,因此我將其更改爲「new google.maps.LatLng」。然後我得到了「marker is undefined」 – user547794 2011-01-27 04:45:56