0

I have need to generate parallel Lines in Google maps. But there is one problem. I can generate points by bearing but i need accurate calculations and this formula isn't accurate it can be of by meters few.

public LatLng linePointByBearingAndDistance(LatLng coords, double bearing, double distance, double degrees)
{
    double R = 6371000; // 6356800;  6371000
    double dist = distance;

    double brng =  bearing + degrees;

    dist = dist/R;  // convert dist to angular distance in radians
    brng = Math.toRadians(brng);  //
    double lat1 = Math.toRadians(coords.latitude) ;
    double lon1 = Math.toRadians(coords.longitude);// * Math.PI / 180;

    double lat2 = Math.asin( Math.sin(lat1)*Math.cos(dist) +
            Math.cos(lat1)*Math.sin(dist)*Math.cos(brng) );
    double lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(dist)*Math.cos(lat1),
            Math.cos(dist)-Math.sin(lat1)*Math.sin(lat2));
    lon2 = (lon2+3*Math.PI) % (2*Math.PI) - Math.PI;  // normalise to -180..+180Āŗ

    return new LatLng( Math.toDegrees(lat2*180), Math.toDegrees(lon2));

}

is there any other solution for generating parallel lines in google maps?

Edit As requested i add some clarification.

My goal is to create parallel lines to selected one. But they should be way longer. So, what I've done, is just set bearing equals to selected line bearing to function that i described and this should give prolonged line. But prolonged line always is a bit to side from initial line.

As result i come with workaround

So i use two function to calculate latLng to normal x;y coordinates by two functions.

public Coordinate ToMercator(LatLng given)
{
    double lon = given.longitude;
    double lat = given.latitude;
    double x = lon * 20037508.34 / 180;
    double y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
    y = y * 20037508.34 / 180;
    return new Coordinate(x,y);
}


public LatLng ToLonLat(Coordinate given)
{
    double x =  given.x;
    double y =  given.y;
    double lon = (x / 20037508.34) * 180;
    double lat = (y / 20037508.34) * 180;
    lat = 180/Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2);
    return new LatLng(lat, lon);
}

Since i need to prolong line i work with coordinates and i get great results from following function.

   public  LatLng [] prolongLine(LatLng l0,LatLng l1, double length )
    {
        Coordinate a = ToMercator(l0);
        Coordinate b = ToMercator(l1);
        Coordinate c = new Coordinate(0,0);
        Coordinate d = new Coordinate(0,0);

        double lengthAB = Math.sqrt( Math.pow((a.x - b.x), 2) + ( Math.pow((a.y - b.y), 2)));
        c.x = b.x + (b.x - a.x) / lengthAB * length;
        c.y = b.y + (b.y - a.y) / lengthAB * length;

        d.x = b.x - (b.x - a.x) / lengthAB * length;
        d.y = b.y - (b.y - a.y) / lengthAB * length;

        return new LatLng[]{  ToLonLat(c), ToLonLat(d) };
    }

And in the end i use the very first function to generate parallel points (they are max 30-40 meters apart).

So as the result i get almost perfect parallel lines.

But the question remains is my method is valid and how to make the whole thing more accurate?

Alpha
  • 1,754
  • 3
  • 21
  • 39
  • It looks like you treat earth as a perfect sphere; the error you get is because earth isn't. Last year I made a javascript object that treats earth as an ellipsoid, it also has a function to move by heading. This also isn't perfectly accurate, but see if this helps. http://stackoverflow.com/questions/27598753/mercator-projection-slightly-off#27604670 – Emmanuel Delay Sep 11 '15 at 09:30
  • Also, there is no such thing as parallel lines on a sphere. Any straight line means you get to the other side of the planet, then you get back to the same spot. So any two (different) lines will cross somewhere. What kind of distances are you talking about? How long is the line and how big is the error? Can you give a few examples of results of your function? – Emmanuel Delay Sep 11 '15 at 09:44
  • we are talking about 200 km max. But problem is that those 200km must be ~10cm accuracy. – Alpha Sep 14 '15 at 12:24

1 Answers1

0

Okay, let's see what you are asking for, in theory.

Let's say the earth is a sphere, circumference = 40000km (which is quite accurate). We put Alice in a boat, and Bob in a boat, both at the equator.

Alice floats at {lat: 0.0, lng: -1.0}, Bob floats at {lat: 0.0, lng: 1.0}.

Distance: 40000 / 360 * 2 = 222.2222 km

Now they both row northwards, they start perfectly parallel; they row 2 degrees northwards (222.222 km). So now Alice floats at {lat: 2.0, lng: -1.0}, Bob floats at {lat: 2.0, lng: 1.0}

New distance Alice-Bob: (40000 / 360 * 2) * cos(lat) = 222.0869 km.

So, you see, they start parallel, but parallel lines on a sphere just don't exist. There are only lines that cross (on two places; in this case in the north pole and south pole).

So even in theory, there is a difference of 135 meter (for an initial distance of 2 degrees). And both boats are not exactly parallel anymore.

Having heard this, is there a way you want to reformulate your question? What dou you expect to happen?

Emmanuel Delay
  • 3,619
  • 1
  • 11
  • 17
  • well i don't create lines on half of earth. – Alpha Sep 15 '15 at 14:42
  • No, like you I took about 200km distances. And there is a 135 meter error. So, like I said, can you give concrete examples? Show me your calculations, show me what goes wrong. And tell me what exactly you expect. – Emmanuel Delay Sep 15 '15 at 14:46
  • 1
    Lets say i have giant spaceship that destroys all life. It works only perpendicular line of it's moving direction (100km width). I need to know where exactly i killed every life form, so i can purge planet in sufficient way? – Alpha Jun 01 '18 at 07:31
  • Spacecraft coordinates are tricky, because everything moves and rotates relative to each other. I think they pick a distant star as x-coordinate. Check out video "Right ascension and declination - sixty symbols" on Youtube – Emmanuel Delay Jun 02 '18 at 10:21
  • 1
    Ok, Then we can change that to Giant walker robot, that kills all life with 100km width. :D – Alpha Jun 04 '18 at 06:53