简体   繁体   English

标记移动时是否重新绘制Google Maps API方向?

[英]Re-drawing Google Maps API directions when a marker is moved?

I am trying to do 2 things: 我正在尝试做两件事:

  • Redraw the route when a marker is moved. 移动标记时,重新绘制路线。
  • Fill in the GPS value of the new customer position when the marker is moved. 当标记移动时,填写新customer位置的GPS值。

I finally got the movable marker working, but it's not updating the value or re-drawing the route. 我终于使活动标记器工作了,但是它没有更新值或重新绘制路线。 What am I missing? 我想念什么?

The HTML: HTML:

<div id="map_canvas" style="width:500px;height:380px;"></div>
<br>
<input type="text" class="form-control" id="customer_location" value="" readonly="true">

The JS: JS:

        var directionDisplay;
                var directionsService = new google.maps.DirectionsService();
                var map;

                function initialize_create_task_map() {
                    directionsDisplay = new google.maps.DirectionsRenderer({
                          suppressMarkers: true
                      });
                    var newyork = new google.maps.LatLng(40.621757, -73.984927);
                    var myOptions = {
                        zoom:14,
                        mapTypeId: google.maps.MapTypeId.ROADMAP,
                        center: newyork
                    }

                    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
                    directionsDisplay.setMap(map);


                    printRoute();

                }

                function printRoute() {

            var distanceInput = document.getElementById("distance");
                    var restaurant = "40.679666, -73.964425";
                    var customer = "40.615758, -74.007652";

                    var locations = [
                                ['Restaurant', 40.679666, -73.964425,'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'],
                                ['Customer', 40.615758, -74.007652,'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png']
                            ];

                    var request = {
                        origin:restaurant, 
                        destination:customer,
                        waypoints: [],
                        travelMode: google.maps.DirectionsTravelMode.WALKING
                    };
                    console.log("Preparing marker...");
                    //add marker to each locations
                            for (i = 0; i < locations.length; i++) {
                            console.log("Adding marker...");
                                marker = new google.maps.Marker({
                                                position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                                                map: map,
                                                draggable:true,
                                                icon: locations[i][3]
                                });

                                google.maps.event.addListener(marker, 'dragend', function() 
                                {
                            distanceInput.value = marker.getPosition();
                                });

                            }

                    directionsService.route(request, function(response, status) {
                        if (status == google.maps.DirectionsStatus.OK) {
                            directionsDisplay.setDirections(response);
                        }
                    });
                }

google.maps.event.addDomListener(window, 'load', initialize_create_task_map);

Live fiddle: https://jsfiddle.net/w1nae25n/2/ 现场提琴: https//jsfiddle.net/w1nae25n/2/

Use this event on dragging marker and get new position and pass to this lat long to new request for directions: 在拖动标记上使用此事件并获得新位置,并长时间传递给新的路线请求请求:

 google.maps.event.addListener(marker, 'dragend', function (event) 
                            {
                       // distanceInput.value = marker.getPosition();
                       console.log(this.getPosition().lat());
                       console.log(this.getPosition().lng());

                            });

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

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