0

Note : PolyLine is a Single line between two marker in my case.

Problem : Simply want to remove a polyLine on MapClick .

As you know there is no way to add a click listener to PolyLine. So i have to use MapClick but i want to remove a PolyLine(simple line) on click .

There are a number of PolyLines drawn between Marker. and i simply want to remove the PolyLine on which i clicked.

My ideas.

  1. There is a way to use a loop and check the nearest Marker and remove the line between them.
  2. One method we study in Mathematics . Point lie on line. When i click on map it check all adjacent marker and check whether this latLng lie on that PolyLine between two Marker.

Both my ideas have some issues/problems. like .

  • in the first one if i clicked on any where it will definetly remove the nearest PolyLine . May be i unintentially clicked anywhere
  • And in the second one I must have to click exactly on the line. If the point(my click) don't lie on it . The line will not remove.

Don't know how to solve such an issue. Give me an idea and also tell me how to implement 2nd method. (Point lie on a line between two markers.)

Or share some resource.

Thanks in Advance..

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300

1 Answers1

0

The problem with point lie on a line is that the polylines are made of points which are then connected. You can't really check if a point is on the polyline rather than to compare the distance from your point and the closest point on the polyline.

To accomplish what you are after you will have to loop through polylines and check which one has a point that is closest to the coordinates of the click. When you've found the polyline, you can call setVisible(false); on the polyline.

In this answer I explained how to loop through polylines and check the distance. I also noted that you can find the closest polyline by continuing the loop through all the polylines.

This can be further enhanced by using algorithms to check if the points are going away or towards your point. If away - then you continue to the next polyline, if towards - you carry on looping.

Community
  • 1
  • 1
Simas
  • 43,548
  • 10
  • 88
  • 116