Google Map API - Directions Service does not auto

2019-07-31 12:09发布

问题:

I have the following function to show direction between 2 lats and lngs to user

$(document).ready(function(){
    initialize_direction();
});

function initialize_direction() {
    var directionDisplay;
    var directionsService = new google.maps.DirectionsService();
    var direction_map;

    directionDisplay = new google.maps.DirectionsRenderer();
    var myOptions = {
        mapTypeId: google.maps.MapTypeId.ROADMAP,
    }
    direction_map = new google.maps.Map(document.getElementById("direction_canvas"), myOptions);
    directionDisplay.setMap(direction_map);

    var start = '51.5074, 0.1278';
    var end = '<%= lat %>' + ',' + '<%= lng %>';
    var request = {
        origin:start,
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionDisplay.setDirections(response);
        }
    });
}

The problem is if I put the canvas that contains the map on the page, the map is automatically zoomed to fit 2 markers but if I put the canvas in a Bootstrap modal, I got the following which does not automatically zoomed to fit markers:

Does anyone know how to fix this? Below is the Bootstrap Modal

<div class="modal fade" id="direction_modal" role="dialog">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Direction to restaurant</h4>
        </div>
        <div class="modal-body">
            <div id="direction_canvas" style="width:100%;height:400px;"></div>
        </div>
    </div>
</div>

回答1:

Call initialize_direction(); on modal open. Try Like below it may help.

 $(document).ready(function(){
	  $( "#direction_modal" ).on('shown.bs.modal', function(){
	    initialize_direction();
	  });
	});

	function initialize_direction() {
	    var directionDisplay;
	    var directionsService = new google.maps.DirectionsService();
	    var direction_map;

	    directionDisplay = new google.maps.DirectionsRenderer();
	    var myOptions = {
	        mapTypeId: google.maps.MapTypeId.ROADMAP,
	    }
	    direction_map = new google.maps.Map(document.getElementById("direction_canvas"), myOptions);
	    directionDisplay.setMap(direction_map);

	    var start = '51.5074, 0.1278';
	    var end = '51.2074, 0.1278';
	    var request = {
	        origin:start,
	        destination:end,
	        travelMode: google.maps.DirectionsTravelMode.DRIVING
	    };
	    directionsService.route(request, function(response, status) {
	        if (status == google.maps.DirectionsStatus.OK) {
	            directionDisplay.setDirections(response);
	        }
	    });
	}
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
   <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key=AIzaSyAmqnSK2LDuPesG7GMG9thy_KDDmKnr7Zk"></script>
 
<div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#direction_modal">Open Modal</button>


    
      <!-- Modal content-->
   <div class="modal fade" id="direction_modal" role="dialog">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Direction to restaurant</h4>
        </div>
        <div class="modal-body">
            <div id="direction_canvas" style="width:100%;height:400px;"></div>
        </div>
    </div>
</div>

  
</div>