Skip to content

Instantly share code, notes, and snippets.

@Omnyyah
Created April 22, 2018 10:47
Show Gist options
  • Save Omnyyah/f47cd66fee479be412329b10adf834d7 to your computer and use it in GitHub Desktop.
Save Omnyyah/f47cd66fee479be412329b10adf834d7 to your computer and use it in GitHub Desktop.
A function to draw a curved path between 2 locations on google map
/drawing curved path but doesnt avoid obstacles
private void drawPath(LatLng userLocation,LatLng doorLocation,double k)
{
//Calculate distance and heading between two points
double d = SphericalUtil.computeDistanceBetween(userLocation,doorLocation);
double h = SphericalUtil.computeHeading(userLocation,doorLocation);
//Midpoint position
LatLng p = SphericalUtil.computeOffset(userLocation, d*0.5, h);
//Apply some mathematics to calculate position of the circle center
double x = (1-k*k)*d*0.5/(2*k);
double r = (1+k*k)*d*0.5/(2*k);
LatLng c = SphericalUtil.computeOffset(p, x, h + 90.0);
//Polyline options
PolylineOptions options = new PolylineOptions();
List<PatternItem> pattern = Arrays.<PatternItem>asList(new Dash(30), new Gap(20));
//Calculate heading between circle center and two points
double h1 = SphericalUtil.computeHeading(c, userLocation);
double h2 = SphericalUtil.computeHeading(c, doorLocation);
//Calculate positions of points on circle border and add them to polyline options
int numpoints = 50;
double step = (h2 -h1) / numpoints;
for (int i=0; i < numpoints; i++) {
LatLng pi = SphericalUtil.computeOffset(c, r, h1 + i * step);
options.add(pi);
}
//Draw polyline
mMap.addPolyline(options.width(10).color(Color.MAGENTA).geodesic(false).pattern(pattern));
}
@Omnyyah
Copy link
Author

Omnyyah commented Apr 22, 2018

calling the function
drawPath(firstLocation,secondLocation,0.4);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment