Javascript 在GoogleMapsDirections API中使用PHP变量

Javascript 在GoogleMapsDirections API中使用PHP变量,javascript,php,json,google-maps,google-api,Javascript,Php,Json,Google Maps,Google Api,我正在使用谷歌提供的代码显示他们的谷歌地图方向API。我已经手动更改了原点和目标值,效果非常好。但是,当尝试用PHP变量替换源和目标值时,映射不会加载 这是我的代码,任何建议都将不胜感激 <body> <?php $start = "Leeds"; $end = "Nottingham"; ?> var directionsService=new google.maps.directionsService(); var directionsD

我正在使用谷歌提供的代码显示他们的谷歌地图方向API。我已经手动更改了原点和目标值,效果非常好。但是,当尝试用PHP变量替换源和目标值时,映射不会加载

这是我的代码,任何建议都将不胜感激

 <body>

  <?php
  $start = "Leeds";
  $end = "Nottingham";
  ?>


var directionsService=new google.maps.directionsService();
var directionsDisplay=new google.maps.DirectionsRenderer();
var map=new google.maps.map(document.getElementById('map'){
缩放:7,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
方向显示.setMap(地图);
directionsDisplay.setPanel(document.getElementById('right-panel');
var请求={
来源:,
目的地:,
travelMode:google.maps.Directions travelMode.DRIVING
};
路由(请求、功能(响应、状态){
if(status==google.maps.directionstatus.OK){
方向显示。设置方向(响应);
}
});

我相信你在PHP中缺少引用。试试这个。 在PHP中使用echo时

$Example = "hey"; 
PHP将只回显引号中的内容。 因此,在JavaScript中,您必须手动添加它们,否则您的结果是错误的

  origin: Start,
  destination: End
这是必须的

 origin: 'Start',
  destination: 'End'

var directionsService=new google.maps.directionsService();
var directionsDisplay=new google.maps.DirectionsRenderer();
var map=new google.maps.map(document.getElementById('map'){
缩放:7,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
方向显示.setMap(地图);
directionsDisplay.setPanel(document.getElementById('right-panel');
var请求={
来源:'',
目标:“”,
travelMode:google.maps.Directions travelMode.DRIVING
};
路由(请求、功能(响应、状态){
if(status==google.maps.directionstatus.OK){
方向显示。设置方向(响应);
}
});

我相信你在PHP中缺少引用。试试这个。 在PHP中使用echo时

$Example = "hey"; 
PHP将只回显引号中的内容。 因此,在JavaScript中,您必须手动添加它们,否则您的结果是错误的

  origin: Start,
  destination: End
这是必须的

 origin: 'Start',
  destination: 'End'

var directionsService=new google.maps.directionsService();
var directionsDisplay=new google.maps.DirectionsRenderer();
var map=new google.maps.map(document.getElementById('map'){
缩放:7,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
方向显示.setMap(地图);
directionsDisplay.setPanel(document.getElementById('right-panel');
var请求={
来源:'',
目标:“”,
travelMode:google.maps.Directions travelMode.DRIVING
};
路由(请求、功能(响应、状态){
if(status==google.maps.directionstatus.OK){
方向显示。设置方向(响应);
}
});

Quote似乎是这里的正确答案
将输出
leeds
,JavaScript希望对象数组中的值像这样引用
'leeds'
。引号也有助于JavaScript理解空格。例如,
origin:New York
会破坏JavaScript,而
origin:“New York”
是有效的。刚刚添加的代码刚刚尝试在PHP中添加引号,似乎仍然无法加载地图。我的代码的起始行和目标行现在格式正确。好的。那么你的代码是好的,是Google API抛出了这个错误:)尝试搜索
Google Maps API init而不是函数。我发现这个()引语看起来是正确的答案
将输出
leeds
,JavaScript希望对象数组中的值像这样引用
'leeds'
。引号也有助于JavaScript理解空格。例如,
origin:New York
会破坏JavaScript,而
origin:“New York”
是有效的。刚刚添加的代码刚刚尝试在PHP中添加引号,似乎仍然无法加载地图。我的代码的起始行和目标行现在格式正确。好的。那么你的代码是好的,是Google API抛出了这个错误:)尝试搜索
Google Maps API init而不是函数。我发现这个()