0

In my Android app I am showing a KML on Google Map. I am also showing device location on the Map.

  1. Now I want to find the point/line on KML which is closest to the device location on the Map. How can achieve that?

  2. Also, I want to know if any KML point/line is within 10 meters of device location.

M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69

1 Answers1

0

Solved. I followed the following steps to solve the 2nd part:

  1. Added function to detect if line-segment collides with circle: ref: https://stackoverflow.com/a/21989358/1397821

Java converted function:

    static boolean checkLineSegmentCircleIntersection(double x1, double y1, double x2 , double  y2, double  xc, double  yc, double r) {

        double xd = 0.0;
        double yd = 0.0;
        double t = 0.0;
        double d = 0.0;
        double dx_2_1 = 0.0;
        double dy_2_1 = 0.0;

        dx_2_1 = x2 - x1;
        dy_2_1 = y2 - y1;

        t = ((yc - y1) * dy_2_1 + (xc - x1) * dx_2_1) / (dy_2_1 * dy_2_1 + dx_2_1 * dx_2_1);

        if( 0 <= t && t <=1) {
            xd = x1 + t * dx_2_1;
            yd = y1 + t * dy_2_1;

            d = Math.sqrt((xd - xc) * (xd - xc) + (yd - yc) * (yd - yc));
            return d <= r;
        }
        else {
            d = Math.sqrt((xc - x1) * (xc - x1) + (yc - y1) * (yc - y1));

            if (d <= r)
                return true;
            else {
                d = Math.sqrt((xc - x2) * (xc - x2) + (yc - y2) * (yc - y2));
                if (d <= r)
                    return true;
                else
                    return false;
            }
        }
    }
  1. Parsed the KML coordinates and passed the coordinates of line segments to this function, like :

    boolean lineInRadius = checkLineSegmentCircleIntersection(points.get(i - 1).latitude, points.get(i - 1).longitude, points.get(i).latitude, points.get(i).longitude, latDecimal, lngDecimal, RADIUS);

Note: your radius can be aprx 0.000009 for 1 meter (https://stackoverflow.com/a/39540339/1397821). This is not exact radius, it'll be oval.

To solve the 1st part, you can edit the above function and find the minimum distance. Check the line d <= r where distance is compared with radius.

M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69