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?