[英]get direction with google maps api without using a map
Hi I'm playing around with a frontend project which will have no backend and I want it to get transit direction from a specific place to another. 嗨,我正在玩一个没有后端的前端项目,我希望它获得从特定位置到另一个位置的运输方向。
The problem I have is that google maps api for browser does always have to use a map if I'm correct. 我的问题是,如果我正确的话,用于浏览器的Google Maps API总是必须使用地图。
I just want to show eg 3 next bus from this bus stop. 我只想显示例如从该巴士站出发的3辆下一班车。
It so much documentation on different things and I'm not sure which api to use. 关于不同事物的文档太多了,我不确定要使用哪个api。 Could I use server api?
我可以使用服务器api吗?
Any google api pro out there who could help me? 有没有可以帮助我的Google API专业人士?
thanks! 谢谢!
A map is not required. 不需要地图。 The Google Maps Javascript API v3 DirectionsService can just render the text directions in the "panel" (an HTML div).
Google Maps Javascript API v3 DirectionsService只能在“面板”(HTML div)中呈现文本方向。
From the documentation : 从文档中 :
setPanel(panel:Node) |
setPanel(panel:Node)| Return Value: None This method renders the directions in a .
返回值:无此方法在中显示方向。 Pass null to remove the content from the panel.
传递null以从面板中删除内容。
code snippet: (modified from this example in the documentation ) 代码段:(从文档中的此示例修改)
function initMap() { var directionsDisplay = new google.maps.DirectionsRenderer; var directionsService = new google.maps.DirectionsService; directionsDisplay.setPanel(document.getElementById('right-panel')); var onChangeHandler = function() { calculateAndDisplayRoute(directionsService, directionsDisplay); }; document.getElementById('start').addEventListener('change', onChangeHandler); document.getElementById('end').addEventListener('change', onChangeHandler); calculateAndDisplayRoute(directionsService, directionsDisplay); } function calculateAndDisplayRoute(directionsService, directionsDisplay) { var start = document.getElementById('start').value; var end = document.getElementById('end').value; directionsService.route({ origin: start, destination: end, travelMode: google.maps.TravelMode.TRANSIT }, function(response, status) { if (status === google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(response); } else { window.alert('Directions request failed due to ' + status); } }); } google.maps.event.addDomListener(window, "load", initMap);
html, body { height: 100%; margin: 0; padding: 0; }
<script src="https://maps.googleapis.com/maps/api/js"></script> <div id="floating-panel"> <strong>Start:</strong> <select id="start"> <option value="chicago, il">Chicago</option> <option value="st louis, mo">St Louis</option> <option value="joplin, mo">Joplin, MO</option> <option value="oklahoma city, ok">Oklahoma City</option> <option value="amarillo, tx">Amarillo</option> <option value="gallup, nm">Gallup, NM</option> <option value="flagstaff, az">Flagstaff, AZ</option> <option value="winona, az">Winona</option> <option value="kingman, az">Kingman</option> <option value="barstow, ca">Barstow</option> <option value="san bernardino, ca">San Bernardino</option> <option value="los angeles, ca">Los Angeles</option> </select> <br> <strong>End:</strong> <select id="end"> <option value="chicago, il">Chicago</option> <option value="st louis, mo" selected="selected">St Louis</option> <option value="joplin, mo">Joplin, MO</option> <option value="oklahoma city, ok">Oklahoma City</option> <option value="amarillo, tx">Amarillo</option> <option value="gallup, nm">Gallup, NM</option> <option value="flagstaff, az">Flagstaff, AZ</option> <option value="winona, az">Winona</option> <option value="kingman, az">Kingman</option> <option value="barstow, ca">Barstow</option> <option value="san bernardino, ca">San Bernardino</option> <option value="los angeles, ca">Los Angeles</option> </select> </div> <div id="right-panel"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.