gpt4 book ai didi

javascript - 如何将值传递给javascript中的函数(googleMap)

转载 作者:行者123 更新时间:2023-11-28 09:39:51 27 4
gpt4 key购买 nike

我正在关注https://developers.google.com/maps/documentation/javascript/directions到目前为止, map 计算特定点之间的距离,但我需要它接收不同的值并根据这些值计算距离,我可以使用选择标签来执行以下操作,但我需要它自动完成,以便当用户打开时在页面上,值应该发送到函数来计算距离。 (我尝试将值传递给 calcRoute 函数,但它不起作用)

另一个问题是如何将列表(用于航路点部分)传递给函数?

  function calcRoute() {
var travel = document.getElementById("mode").value; << currently it is done manually
var origin = document.getElementById("origin").value; << manually
var request = {
origin: origin,
destination: '238 Elizabeth St,Los Angeles, CA',
waypoints:[{location: "George Street,Los Angeles,CA"}],
travelMode: travel
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}

最佳答案

尝试如下更改 calcRoute 函数定义。

function calcRoute(){
var travel = document.getElementById("mode").value;
var origin = document.getElementById("origin").value;

function calcRoute(x,y){
if(x && y){
// your code to process your parameter values
var travel = x
var origin = y
}else{
// your code to process, if no parameters given (for default functionality)
var travel = document.getElementById("mode").value; << currently
var origin = document.getElementById("origin").value; << manually
}

当你想传递不同的值时,尝试调用带参数的 calcRoute 函数

希望有帮助:)

关于javascript - 如何将值传递给javascript中的函数(googleMap),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12541179/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com