使用Google Directions API将PHP变量添加到Javascript函数中

时间:2017-09-18 21:46:54

标签: javascript php api google-maps google-direction

我使用谷歌API相当新。我正在创建一个应用程序,我需要使用保存在数据库中的地址t0显示到事件的路径。我找到了一个允许我手动更改google maps API中的Javascript变量的模板,但是当我尝试在函数中使用PHP变量时,它不会加载地图。我还没有将它连接到我的数据库,我只是想让它使用PHP变量

只是想知道是否有人能指出我正确的方向并帮助我。

saveDF.write.option("header", "true").csv("pre-processed")

1 个答案:

答案 0 :(得分:0)

我已经审核了您的代码,并且看起来您将原点和目标值传递为" STRING CONATING THE PLACE NAME" ,在谷歌地图API中,它将以谷歌地图api https://developers.google.com/maps/documentation/javascript/examples/directions-travel-modes中所述的方式接收地点的坐标,纬度和长度:

origin: {lat: 37.77, lng: -122.447},  // Haight.
destination: {lat: 37.768, lng: -122.511},  // Ocean Beach.
// Note that Javascript allows us to access the constant
// using square brackets and a string value as its
// "property."
travelMode: google.maps.TravelMode[selectedMode]

所以你应该替换以下代码:

 var request = {
       origin: <?php echo $start; ?>,
       destination: <?php echo $end; ?>,
       travelMode: google.maps.DirectionsTravelMode.DRIVING
     };

使用类似于我上面发布的代码。 希望这会对你有所帮助。