开发者

google maps api v3 center map after reading xml file

I'm in the process of updating my google maps code to version 3 and i've come across a problem.

In version 2, I was reading in an xml file to create a marker and I was centering my map based on the coordinates, but in version 3 the center has been defined in the map variable before the xml file has been read.

Is this easy to fix?

Version 3 code taken from http://code.google.com/apis/maps/articles/phpsqlajax_v3.html

function load() {
  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(47.6145, -122.3418),
    zoom: 13,
    mapTypeId: 'roadmap'
  });
  var infoWindow = new google.maps.InfoWindow;

  // Change this depending on the name of your PHP file
  downloadUrl("results.xml", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var name = markers[i].getAttribute("event");
      var address = markers[i].getAttribute("location");
      var type = markers[i].getAttribute("type");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
      var html = "<b>" + nam开发者_StackOverflowe + "</b> <br/>" + address;
      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon,
        shadow: icon.shadow
      });
      bindInfoWindow(marker, map, infoWindow, html);
    }
  });
}


Maybe you mean map.setCenter(latlng:LatLng) ?Parse your xml, create the markers,then center the map where you want.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜