2013-10-01 146 views
0

我有一个代码:刷新谷歌地图API的路线

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); 
    } 
}); 
} 

但显然这是行不通的。

+0

您是否获得'loc'和'des'价值观 –

+0

首先深入阅读文本:) loc = $('input [name = location]')。val(); des = $('input [name = destination]')。val(); –

+0

我可以看到,我的问题是,你是否在'loc'和'dec'变量中获得值?你试过提醒还是安慰它? –

回答

1

你缺少,

directionsDisplay.setMap(map); 
在你的第二个功能 renewMap()

,可能是因为这一点,并没有更新