开发者

Google Maps V3 - Loop through markers and zoom map to last marker

开发者 https://www.devze.com 2023-02-07 12:11 出处:网络
I have a function that loops through an XML file of marker data.I need to set my map so that the last marker in the loop is zoomed in.

I have a function that loops through an XML file of marker data. I need to set my map so that the last marker in the loop is zoomed in.

    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, "clic开发者_如何学运维k", function() {
      if (infowindow) infowindow.close();
      infowindow = new google.maps.InfoWindow({content: html});
      infowindow.open(map, marker);
    });
    return marker;
  }


try to use this to center the marker

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

精彩评论

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