1
我有一個具體的要求,我必須在地圖上而不是整個地圖上顯示鐵路車站。這怎麼能實現呢?請找到我嘗試過的下面的代碼。利用谷歌地圖API中的鐵路線路
<html>
<head>
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0px; padding: 0px }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?libraries=drawing&sensor=true">
</script>
<script type="text/javascript">
function initialize() {
// init map
var myOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var drawingManager = new google.maps.drawing.DrawingManager();
drawingManager.setMap(map);
// init directions service
var dirService = new google.maps.DirectionsService();
var dirRenderer = new google.maps.DirectionsRenderer({suppressMarkers: true});
dirRenderer.setMap(map);
// highlight a street
// highlight a street
var request = {
origin: '',
destination: '',
travelMode: google.maps.TravelMode.TRANSIT
};
dirService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
dirRenderer.setDirections(response);
var legs = response.routes[0].legs;
for (i = 0; i < legs.length; i++) {
var steps = legs[i].steps;
for (j = 0; j < steps.length; j++) {
var transitMode = steps[j].travel_mode;
if (transitMode == "TRANSIT") {
var vehicle = steps[j].transit.line.vehicle.type;
if (vehicle == "HEAVY_RAIL") {
var nextSegment = steps[j].path;
for (k = 0; k < nextSegment.length; k++) {
// polyline.getPath().push(nextSegment[k]);
}
}
}
}
}
}
});
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
任何幫助將非常感激..在此先感謝..
你的建議可以爲事業服務。我想要的是顯示鐵路地圖。只有火車站可以展示。我確實實現了transit.js,但它似乎沒有工作 – sTg
你的意思是你想在站上放置標記嗎?或者,您可以在每個站的連線上創建帶有矢量形狀的疊加層。你是否已經在'transit.initialize()'方法中正確設置了'localKmlFile'和'remoteKmlFile'? http://onloop.net/transit/#initialize在問題中添加新的代碼,我們將檢查它。 – tuned