编程语言
首页 > 编程语言> > javascript-点击即可在Google地图上绘制路线

javascript-点击即可在Google地图上绘制路线

作者:互联网

我想做的是单击Google地图,放置一个标记,并在这些标记之间绘制一条路线.这可行,但是我面临的问题如下:(请查看地图图像)

(1)当我在Google地图上单击三下时,路线被完美绘制. (这意味着我首先单击A,然后单击B,然后单击C位置)

(2)然后,如果我在Google地图上单击第四次,该位置必须为D,但地图看起来如下:

在此B位置被替换或隐藏,并且在B处显示C,并且相同…

(3)如果我第五次单击Google地图,则位置应为E,但地图看起来像:

简而言之,如果我在Google地图上单击五次,则路线应为“ A,B,C,D,E”.而是将其显示为“ A,E,F,G,H”.

完整的代码如下:

<script>
    var waypts=[];
    var ways=[];
    function initialize() {
        var geocoder = new google.maps.Geocoder();
        var mapOptions = {
                            zoom: 11,
                            center: new google.maps.LatLng(23.0171240, 72.5330533),
                            mapTypeId: google.maps.MapTypeId.ROADMAP
                         };
        var map = new google.maps.Map(document.getElementById('map_canvas'),mapOptions);

        google.maps.event.addListener(map, 'click', function(e) {
            placeMarker(e.latLng, map);
            var input=e.latLng;
            var lat = parseFloat(input.lat());
            var lng = parseFloat(input.lng());
            var latlng = new google.maps.LatLng(lat, lng);
            geocoder.geocode({'latLng': latlng}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    var add=results[1].formatted_address;
                    waypts.push({
                        location:add,
                        stopover:true
                    });
                    //alert(JSON.stringify(waypts));
                    if(waypts.length>2) {
                        for ( var i = 1; i <= waypts.length - 2 ; i++) {
                            ways.push({
                                location:waypts[i].location,
                                stopover:true
                            });
                        }
                    }
                    makeroute(waypts);
                }
            });
        });

        <!--  **************    for route between markers   *******************  -->
        function makeroute(waypts){
            var directionsDisplay;
            var directionsService = new google.maps.DirectionsService();
            directionsDisplay = new google.maps.DirectionsRenderer({
                suppressMarkers: false, //false it if you want a marker from the direction service
                polylineOptions: {
                    strokeColor: 'green', //"black",
                    strokeOpacity: 1.0,
                    strokeWeight: 3
                }
            });

            var start = waypts[0].location;//"Bopal, Ahmedabad, Gujarat, India";
            var end = waypts[waypts.length-1].location;//"Nikol, Ahmedabad, Gujarat, India";

            if(waypts.length>1) {
                var request = {
                                 origin:start,
                                 destination:end,
                                 waypoints:ways,
                                 travelMode: google.maps.DirectionsTravelMode.DRIVING
                               };
                directionsService.route(request, function(response, status) {
                    if (status == google.maps.DirectionsStatus.OK) {
                        directionsDisplay.setDirections(response);
                    }
                });

                directionsDisplay.setMap(map);
                google.maps.event.addDomListener(window, 'load', initialize);
            }
        }
    }

    function placeMarker(position, map) {
        var marker = new google.maps.Marker({
            position: position,
            //map: map
        });
        // map.panTo(position);
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>

最新更新

我使用directionsDisplay.setPanel(document.getElementById(‘a’));有关完整的路线信息.

每当我点击谷歌地图时,它就会显示出来,重复路线.意思是第一次单击A,第二次单击A到B,第三次A到B到C而不是B到C等.它还显示某些点已合并.例如,B和C具有相同的位置,并且仅在地图上显示C.

解决方法:

每次添加新点时,您都​​需要清除航点数组(“ ways”).每次添加一个点时,起点都保持不变,但是需要将旧的终点添加到路点数组中,而新的点就是终点.当前,您永远不会清除“ ways”数组.改变这个:

  if(waypts.length>2) {
    for ( var i = 1; i <= waypts.length - 2 ; i++) {
       ways.push({
         location:waypts[i].location,
         stopover:true
       });
    }
  }
  makeroute(waypts);

对此:

  if(waypts.length>2) {
    ways = [];
    for ( var i = 1; i <= waypts.length - 2 ; i++) {
       ways.push({
         location:waypts[i].location,
         stopover:true
       });
    }
  }
  makeroute(waypts);

为我工作.

标签:google-maps,google-maps-api-3,javascript
来源: https://codeday.me/bug/20191122/2061292.html