4

Hi I have connection between marker with polyline like this Image . I am attaching a sample here.enter image description here

How Can I make drag possible that when I drag the the marker with polyline.

example , If I drag the marker 3 it should also update the polyline point and where ever I put the marker 3 polyline should connect with marker 3.

I need this type of drag event that can update the polyline also when dragging the marker.

I am using leaflet for this purpose but still unable to solve the dragging logic of marker with polyline.

Here is the sample code I am using

$http.get("db/getConnectionData.php").then(function (response) {


$scope.links1 = response.data.records;


// $scope.showArrow();
   angular.forEach($scope.links1, function(value, i) {
        var source_panoId = $scope.links1[i].s_panoId;
        var dest_panoId   = $scope.links1[i].d_panoId;
        var sPanoID      = $scope.links1[i].sourcePano_id;
        var dPpanoID     = $scope.links1[i].destPano_id;


          angular.forEach($scope.panoramas, function(value, index) {
              if($scope.panoramas[index].panoId == source_panoId){
                   if($scope.links.indexOf($scope.panoramas[index])== -1){
                         $scope.links.push($scope.panoramas[index]);
                     }
                    var SlatLang = $scope.panoramas[index].project_latLng ;
                    var SLatLngArr = SlatLang.split(",");
                    var Slat  = parseFloat(SLatLngArr[0]);
                    var Slang = parseFloat(SLatLngArr[1]);
                    var polypoint1 = [Slat, Slang];

                angular.forEach($scope.panoramas, function(value, index1) {
                   if($scope.panoramas[index1].panoId == dest_panoId){         
                       if($scope.links.indexOf($scope.panoramas[index1])== -1){                                      
                            $scope.links.push($scope.panoramas[index1]);
                        }
                        var DlatLang = $scope.panoramas[index1].project_latLng ;
                        var DLatLngArr = DlatLang.split(",");
                        var Dlat  = parseFloat(DLatLngArr[0]);
                        var Dlang = parseFloat(DLatLngArr[1]);
                        var polypoint2 = [Dlat, Dlang];

                       // Draw seperate polyline for each connection
                        polyline = L.polyline([[Slat, Slang],[Dlat, Dlang]],
                                              {
                                                color: 'blue',
                                                weight: 5,
                                                opacity: .7,
                                              }
                                              ).addTo(map);

                                    $scope.polycoords.push(polyline);


                                }

                            });


                        }

                    });

Here is the code that I am using to make drag of marker with polyline

angular.forEach($scope.panoramas, function(value, index4){

$scope.markers[index4].on('dragstart', function(e){

            var latlngs = polyline.getLatLngs(),
                latlng = $scope.markers[index4].getLatLng();

            for (var i = 0; i < latlngs.length; i++) {
                if (latlng.equals(latlngs[i])) {
                    this.polylineLatlng = i;
                }
            }



        });//dragstart

        $scope.markers[index4].on('drag', function(e){

          var latlngs = polyline.getLatLngs(),
              latlng = $scope.markers[index4].getLatLng();
              latlngs.splice(this.polylineLatlng, 1, latlng);
              polyline.setLatLngs(latlngs);
        });//drag

        $scope.markers[index4].on('dragend', function(e){
          delete this.polylineLatlng;
        });//dragEnd

    });
upendra
  • 339
  • 4
  • 20

2 Answers2

2

First, when creating the marker, remember to pass the draggable option as true, like this:

var marker = L.marker(latLng, { draggable: true });

Now, check which drag event you want to attach a listener to and then call the redraw function of the polyline inside the callback, like this:

// var polyline defined somewhere
marker.on('drag', function (e) {
    polyline.redraw();
});

If this doesn't work, please provide sample code so we can work around with it.

Edit

You also need to change the coordinates of the polyline, otherwise redraw will do nothing. Check out this answer on SO and see if it fits your needs.

Edit 2

You're using an array of polylines while the answer just uses one polyline which has the array of coordinates, so in your case you need to use two loops to accomplish the same task. You can make this faster and maybe use an object as a lookup table to get the right polyline for each marker, for example, like this:

var table = {};
// ...
table[marker] = polyline;

Then later you can get the polyline used for each marker. But anyway, here's what I think would work in your case the way it is in the sample (it was a little hard to understand but I hope it works for you).

I don't know where you are putting the second part of your sample (the event handlers) but I assume it's not inside the double loop that is creating the polylines, right? So this is what I came up with:

marker.on('dragstart', function (e) {
    var markerLatLng = marker.getLatLng();

    this.polylineLatLngs = [];
    for (var i = 0; i < $scope.polycoords.length; i++) {
        var polyline = $scope.polycoords[i];
        var latLngs = polyline.getLatLngs()

        for (var j = 0; j < latLngs.length; j++) {
            if (markerLatLng.equals(latLngs[j])) {
                this.polylineLatLngs.push([i, j]);
            }
        }
    }
});

marker.on('drag', function (e) {
    for (var i = 0; i < this.polylineLatLngs.length; i++) {
        var polyline = $scope.polycoords[this.polylineLatLngs[i][0]];
        var latLngs = polyline.getLatLngs();
        var markerLatLng = marker.getLatLng();

        latLngs.splice(this.polylineLatLngs[i][1], 1, markerLatLng);
        polyline.setLatLngs(latLngs);
    }
});
Roberto Soares
  • 244
  • 1
  • 11
  • I have the draggable :true , the solution you provided is not working – upendra Aug 22 '17 at 07:20
  • https://stackoverflow.com/questions/33513404/leaflet-how-to-match-marker-and-polyline-on-drag-and-drop I have also followed the same link but I am unable to set it according to my needs – upendra Aug 22 '17 at 07:33
  • This sample is storing all the polycoords in array and then passing it to the polyline . But In my case I have different structure of connecting the polyline. like 1->2 , 1->3 , 1->4 , 4->5 something like this , How can I follow with this link if I have the given type of connectins – upendra Aug 22 '17 at 07:35
  • I believe you can use the same idea of that answer but when the drag starts you need to check which polyline is part of that marker and then use the polyline array of that marker for the rest of the operations. – Roberto Soares Aug 22 '17 at 07:47
  • I tried but could not solve ,, I will give it try again and let you know about the situations. – upendra Aug 22 '17 at 08:19
  • Ok, good luck. If you can put your sample code in a fiddle I can help you out. Edit: Sorry I just realized you edited the question and put it there. Will take a look. – Roberto Soares Aug 22 '17 at 08:22
  • Hi, Solution is not working as expected if 1->2 , 1->3 If I will drag 1 it is only updating single line 1->2 . And 1->3 line is remaining as it is. – upendra Aug 22 '17 at 11:43
  • showing the error - this.polylineLatLngs.append is not a function – upendra Aug 22 '17 at 14:00
  • https://stview.agencia.co.jp/main/moderator/moderator.js Here is my complete source code please check , thank you – upendra Aug 22 '17 at 14:08
  • Thank you brother . you made my day. – upendra Aug 22 '17 at 16:45
  • [Roberto Soares](https://stackoverflow.com/users/4938655/roberto-soares) , how to delete a polyline on double click ? – upendra Aug 23 '17 at 03:39
  • Use the [removeLayer](http://leafletjs.com/reference-1.2.0.html#map-removelayer) method passing the polyline. Attach an event listener to the [dblclick](http://leafletjs.com/reference-1.2.0.html#polyline-dblclick) event on the polyline. You should ask a new question for that. – Roberto Soares Aug 23 '17 at 08:08
0

I am getting this type of behavior. Please let me know how I can solve this .

Thank you for your time.

enter image description here

This is the polyline created by getting data from db or by making the connection between panorama.

enter image description here

This Image when I start dragging the marker 2 I got the result like this

enter image description here

This image when I dragged the marker 3. This type of result I am getting using the source code you provided above.

upendra
  • 339
  • 4
  • 20