简体   繁体   English

谷歌地图javascript:在半地图上显示路线

[英]Google maps javascript: Showing route on half map

I'm working on a webpage which has to show google maps in the background and a form on left half of the page and the right half should show the requested route.我正在处理一个网页,该网页必须在背景中显示谷歌地图,页面左半部分的表格和右半部分应显示请求的路线。 please know that the google map will cover 100% width of the page.请知道谷歌地图将覆盖页面的 100% 宽度。 Here is the complete query picture:这是完整的查询图片:

Query description查询说明

Any ideas how should I accomplish this?任何想法我应该如何做到这一点? In my view it should be done with LatLngBounds, extending route bounds to some value to the left but I'm not sure how to calculate that exact value so that the route is placed on desired place on the map..在我看来,它应该使用 LatLngBounds 完成,将路线边界扩展到左侧的某个值,但我不确定如何计算该确切值,以便将路线放置在地图上所需的位置。

Thanks in advance.提前致谢。

I would suggest:我会建议:

  1. get the bounds of the route (or get the map viewport after the direction service autozooms the map.获取路线的边界(或在方向服务自动缩放地图后获取地图视口。

  2. zoom the map out 1 time (so the viewport is twice the size needed to show the route)将地图缩小 1 次(因此视口是显示路线所需大小的两倍)

  3. center the map on the center of the left size of the bounds of the route.将地图置于路线边界左侧大小的中心。

proof of concept fiddle概念证明小提琴

code snippet:代码片段:

 var map; function initMap() { var directionsService = new google.maps.DirectionsService; var directionsDisplay = new google.maps.DirectionsRenderer({ preserveViewport: true }); map = new google.maps.Map(document.getElementById('map'), { zoom: 0, center: { lat: 41.85, lng: -87.65 } }); directionsDisplay.setMap(map); 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) { directionsService.route({ origin: document.getElementById('start').value, destination: document.getElementById('end').value, travelMode: 'DRIVING' }, function(response, status) { if (status === 'OK') { var bounds = response.routes[0].bounds; var leftSide = new google.maps.LatLng(bounds.getCenter().lat(), bounds.getSouthWest().lng()); var marker = new google.maps.Marker({ position: leftSide, map: map }); console.log(leftSide.toUrlValue(6)); google.maps.event.addListenerOnce(map, 'bounds_changed', function() { map.setZoom(map.getZoom() - 1); map.setCenter(leftSide); }); map.fitBounds(bounds); directionsDisplay.setDirections(response); } else { window.alert('Directions request failed due to ' + status); } }); }
 html, body { height: 100%; margin: 0; padding: 0; } #map { height: 100%; } #floating-panel { position: absolute; top: 25%; left: 10px; z-index: 5; background-color: #fff; padding: 5px; border: 1px solid #999; text-align: center; font-family: 'Roboto', 'sans-serif'; line-height: 30px; padding-left: 10px; height: 50%; opacity: 0.5; }
 <div id="floating-panel"> <b>Start: </b> <input id="start" value="New York, NY" /> <br><b>End: </b> <input id="end" value="Boston, MA" /> <br> <input id="dirbtn" value="get directions" type="button" /> </div> <div id="map"></div> <!-- Replace the value of the key parameter with your own API key. --> <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"> </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM