0

我有一个代码:

var directionsService = new google.maps.DirectionsService();
 var directionsDisplay = new google.maps.DirectionsRenderer();

 var map = new google.maps.Map(document.getElementById('map'), {
   zoom:6,
   mapTypeId: google.maps.MapTypeId.ROADMAP
 });

 directionsDisplay.setMap(map);


 var request = {
   origin: '<?= $_GET['from']; ?>', 
   destination: '<?= $_GET['to']; ?>',
   travelMode: google.maps.DirectionsTravelMode.DRIVING
 };

 directionsService.route(request, function(response, status) {
   if (status == google.maps.DirectionsStatus.OK) {
  route=Math.round(parseFloat(response.routes[0].legs[0].distance.value)*0.002);
   }
 });

当我在两个输入中输入新的起点和目的地时,我需要让谷歌地图刷新路线,我正在考虑:

function renewMap() {
loc=$('input[name=location]').val();
des=$('input[name=destination]').val();

 var request = {
   origin: loc, 
   destination: des,
   travelMode: google.maps.DirectionsTravelMode.DRIVING
 };

 directionsService.route(request, function(response, status) {
   if (status == google.maps.DirectionsStatus.OK) {
  route=Math.round(parseFloat(response.routes[0].legs[0].distance.value)*0.002);
   }
 });
}

但显然这行不通。

4

1 回答 1

1

你失踪了,

   directionsDisplay.setMap(map);

在您的第二个功能renewMap()中,可能是因为它没有更新

于 2013-10-01T08:32:00.290 回答