我已经在我的应用程序中使用路线集成了Google地图,但我只获得了加载的终点。我无法加载起点和中点。
这是我的代码:
<?php $final_input_string = ''; ?>
<?php foreach ($riderRoute as $rider) { ?>
<tr>
<?php $currentTime = Yii::$app->Common->formatDateOnlyUS($rider->currentTime); ?>
<td><?php echo $currentTime; ?></td>
<td><?php echo $rider->lat; ?></td>
<td><?php echo $rider->lng; ?></td>
<td><img src="http://maps.google.com/maps/api/staticmap?center=<?php echo $rider->lat; ?>,<?php echo $rider->lng; ?>&zoom=14&size=600x400&maptype=roadmap&sensor=false&language=&markers=color:red|label:none|<?php echo $rider->lat; ?>,<?php echo $rider->lng; ?>" width="400" height="180" style="border:1px solid #CECECE;"> </td>
</div>
</tr>
<?php $final_input_string = ($final_input_string == '') ? $final_input_string . $rider->lat . ',' . $rider->lng : $final_input_string . '|' . $rider->lat . ',' . $rider->lng; ?>
<?php } ?>
<td><div id="map" style="width: 500px; height: 300px"></div></td>
</table>
</div>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
var map = null;
var infowindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
var markers = [
<?php
$long = $rider->lng;
$lat = $rider->lat;
$routes = array("lng" => $long, "lat" => $lat);
$route_points = array($routes);
foreach ($route_points as $points) {
?>
{
"lat": <?php echo "'" . $points['lat'] . "'"; ?>,
"lng": <?php echo "'" . $points['lng'] . "'"; ?>,
},
<?php
}
?>
];
window.onload = function () {
var mapOptions = {
center: new google.maps.LatLng(
parseFloat(markers[0].lat),
parseFloat(markers[0].lng)),
minZoom: 1,
maxZoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var infoWindow = new google.maps.InfoWindow();
var lat_lng = new Array();
var latlngbounds = new google.maps.LatLngBounds();
for (i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
lat_lng.push(myLatlng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
});
latlngbounds.extend(marker.position);
(function (marker, data) {
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
});
})(marker, data);
}
map.setCenter(latlngbounds.getCenter());
map.fitBounds(latlngbounds);
//***********ROUTING****************//
//Intialize the Path Array
var path = new google.maps.MVCArray();
//Intialize the Direction Service
var service = new google.maps.DirectionsService();
//Set the Path Stroke Color
var poly = new google.maps.Polyline({map: map, strokeColor: '#4986H7'});
//Loop and Draw Path Route between the Points on MAP
for (var i = 0; i < lat_lng.length; i++) {
if ((i + 1) < lat_lng.length) {
var src = lat_lng[i];
var des = lat_lng[i + 1];
path.push(src);
poly.setPath(path);
service.route({
origin: src,
destination: des,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {
path.push(result.routes[0].overview_path[i]);
}
}
});
}
}
}
</script>
我的输出: