Search code examples
javascriptreactjsgoogle-maps-markersgoogle-polylinegoogle-maps-react

bind draggable marker with polyline google map


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

I have added polyline with marker.i'am drawing polyline and marker on 'click' event. basicaly markers are with numbered path. what i actualy want - i'am able to edit polylines and markers separately but i want to bind marker with polyline. when i dragging marker poyline should also drag with marker. you can se my code


Solution

  • I have solved this issue using marker 'drag' event. on drag event i'am redrawing the Polyline with new path. like that way.

       google.maps.event.addListener(marker, "drag", (mark) => {
          let lat = mark.latLng.lat().toString();
          let lng = mark.latLng.lng().toString();
          this.setState((state) => ({
            ...state,
            lattitude: lat,
            longitude: lng,
          }));
          let newPath = polyPath;
          newPath[marker.index-1] = mark.latLng;
          poly.setMap(null);
          poly = new google.maps.Polyline({
            strokeColor: "orange",
            strokeOpacity: 1.0,
            strokeWeight: 5,
            path: newPath,
            geodesic:false,
          });
          poly.setMap(map);
      });