开发者

directionsService.route() with new Map and setMap() in callback breaks setPanel()?

Using the V3 Google Maps API, I've run into a case where it seems that creating the map for .setMap() causes .setPanel() to not do anything. The ultimate goal is to have the map load once. If I create the Map prior to the route call, everything works fine. But I first see the map that is loaded when the Map is created, then the .route callback causes the map to load a second time. I want to eliminate that first load or minimize the time gap. Here's the snippet that works:

  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  directionsService.route(parms, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setMap(map);
      directionsDisplay.setDirections(result);
      directionsDisplay.setPanel(document.getElementById("textlist"));
    }
  });

Here's the broken:

  directionsService.route(parms, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
      directionsDisplay.setMap(map);
      directionsDisplay.setDirections(result);
      directionsDisplay.setPanel(document.getElementById("textlist"));
    }
  开发者_C百科});


you probably should listen in to the load event for the new map that you define in the callback for directionsService.route(). I think you can't render directions until the map is loaded. So the new code would look like:

directionsService.route(parms, function(result, status) { if (status == google.maps.DirectionsStatus.OK) { map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  google.maps.event.addListener(map,'tilesloaded',function(){
      directionsDisplay.setMap(map);
      directionsDisplay.setDirections(result);
      directionsDisplay.setPanel(document.getElementById("textlist"));
  }
};

});


All You need to do is comment the lines map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); and it would not load the first time. I tried the same in my app and it worked for me

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜