Created
April 22, 2018 10:47
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/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)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
calling the function
drawPath(firstLocation,secondLocation,0.4);