您好我试图让谷歌的方向与地理位置一起工作,我已完成地理定位部分,并且它与方向链接并且工作正常,问题是当我尝试使其动态以便它获取不同对象的路线对于不同的页面,我只是无法让它工作我thoguht我可以jsut回应命运语言中的变量,这将是它但它不起作用,在源代码中,destintion是变量而不是内容。继承我的代码
<?php $rows = mysql_fetch_array($results) or die(mysql_error());
echo '<script>';
echo 'if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(function (position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var coords = new google.maps.LatLng(latitude, longitude);
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions ={
zoom: 15,
center: coords,
mapTypeControl: true,
navigationControlOptions:{
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById(\'panel\'));
var request = {
origin: coords,';
$geo = $rows['GEO'];
echo 'destination: \'$geo\',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
});
}
google.maps.event.addDomListener(window, \'load\', initialize);';
echo '</script>';
?>
如果有人能看到我做错了什么,请告诉我,谢谢你的帮助。
修改
我的查询(在其中工作显示页面上的其他所有内容)
$id = $_GET["id"] ;
$sql = "SELECT * FROM Posts where id = $id" ;
$results = mysql_query($sql) or die(mysql_error()) ;
在数据库中GEO不是地理位置,它只是一个城镇或商店等名称。
答案 0 :(得分:2)
如果用&#39;回显,则忽略Php变量。使用&#34;像这样:
$geo = $rows['GEO'];
echo 'destination: '."\'$geo\'".',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
答案 1 :(得分:2)
您遇到的问题是使用单引号字符串,但您应该考虑使用HEREDOC格式。
这将为您提供正确的字符串中的$geo
:
echo "destination: '$geo',";
使用HEREDOC通常更容易阅读:
$geo = $rows['GEO'];
$html = <<<HTML
<script>
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(function (position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var coords = new google.maps.LatLng(latitude, longitude);
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions = {
zoom: 15,
center: coords,
mapTypeControl: true,
navigationControlOptions:{
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('panel'));
var request = {
origin: coords,
destination: '$geo',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
HTML;
echo $html;