此代码是为我编写的并且用于工作,但现在多个航路点无效。 航点在数据库中存储为 | 51.105166,-1.695971 | 51.105166,-1.695971 | 。 只有1个航点,地图可以工作,但是有多个航点根本没有出现。
有人有任何建议吗?
可以在此链接查看。 http://www.ridersguide.co.uk/2012/Ride_234
var directionDisplay;
var geocoder;
var directionsService = new google.maps.DirectionsService();
var latlng = new google.maps.LatLng(54.559322587438636, -4.1748046875);
function load() {
geocoder = new google.maps.Geocoder();
directionsDisplay = new google.maps.DirectionsRenderer();
var myOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: latlng,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
position: google.maps.ControlPosition.TOP_RIGHT
},
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.ZOOM_PAN,
position: google.maps.ControlPosition.TOP_LEFT}
};
var map = new google.maps.Map(document.getElementById('map'), myOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("directionsPanel"));
var directionRendererOptions ={
suppressMarkers: true,
polylineOptions:{
strokeColor: "#FF0000",
strokeOpacity: 1,
strokeWeight: 3
}
};
directionsDisplay.setOptions(directionRendererOptions);
var start = '<?php echo $start; ?>';
var end = '<?php echo $end; ?>';
<?php
if($via != null){
echo "var points = [";
foreach($via as $point){
if($point != ""){
echo "{location:";
echo " '".$point."'";
echo "}";
}
}
echo "];\n";
}
?>
var request = {
origin:start,
waypoints: points,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
geocoder.geocode( { 'address': start}, function(results, status) {
var routeStart = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
icon: './images/motorcycling.png',
shadow: './images/motorcycling.shadow.png'
});
});
geocoder.geocode( { 'address': end}, function(results, status) {
var routeEnd = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
icon: './images/motorcyclingend.png',
shadow: './images/motorcycling.shadow.png'
});
});
<?php`
答案 0 :(得分:0)
您的数组中似乎缺少逗号。
var points = [{location: '51.105166,-1.695971'}{location: '51.105166,-1.695971'}];
应该是
var points = [{location: '51.105166,-1.695971'},{location: '51.105166,-1.695971'}];
这样做的一种方法是完全在php中构造该字符串而不是逐个输出它,然后删除不需要的初始逗号。我已根据收到的评论编辑了此代码,因此只构建了[]之间的位置并删除了逗号:
<?php
if($via != null){
$pointstring='';
foreach($via as $point){
if($point != ""){
$pointstring.= ",{location:";
$pointstring.= " '".$point."'";
$pointstring.= "}";
}
}
echo "var points = [".substr($pointstring,1)."];\n";
}
?>
(未经过测试的代码)
还有其他方式可以像往常一样逐个输出位置,但要求在它们之间使用逗号而不是最后使用逗号会使其比仅删除不需要的位置更复杂。