点击谷歌地图上画一条路线

时间:2013-10-16 09:23:19

标签: javascript google-maps google-maps-api-3

我要做的是点击谷歌地图,放置标记,并在这些标记之间绘制路线。这有效,但我面临的问题如下:(请查看地图图片)

(1)当我在谷歌地图上点击三次时,路线被完美地绘制。 (意思是首先我点击A然后点击B,然后点击C位置

enter image description here

(2)然后,如果我在谷歌地图上点击第四次,该位置必须是D,但地图如下所示:

此处B位置被替换或隐藏,代替B,显示C并且相同... enter image description here

(3)如果我在谷歌地图上第五次点击,该位置应为E,但地图如下所示:

enter image description here

简而言之,如果我在谷歌地图上点击五次,路线应该是'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 to B,第三次A to B to C而不是B to C等时的含义。它还会显示某些点已合并。例如,B and C具有相同的位置,并且仅在地图C上显示。

1 个答案:

答案 0 :(得分:3)

每次添加新点时,都需要清除路点数组(“方式”)。每次添加一个点时,起点保持不变,但旧的终点需要添加到路点数组,新点是终点。目前你永远不会清除“方式”数组。改变这个:

  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);

适合我。