Draw Arc Polyline In Google Map
Solution 1:
The solution that I proposed in another question was focused on curved polylines for really small distances. For example, when you have a Directions API route where the start and end points are snapped to road, but the real start and end points in original request were the positions of buildings, so you can connect the road and building with this curved dashed polyline.
In case of big distances like in your example, I believe you can just use a geodesic polylines that are available in the API. You can set geodesic property of polyline options to true.
publicvoidonMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(true);
LatLnglatLng1=newLatLng(40.7128, 74.0059); // New YorkLatLnglatLng2=newLatLng(51.5074, 0.1278); // LondonMarkermarker1= mMap.addMarker(newMarkerOptions().position(latLng1).title("Start"));
Markermarker2= mMap.addMarker(newMarkerOptions().position(latLng2).title("End"));
List<PatternItem> pattern = Arrays.<PatternItem>asList(newDash(30), newGap(20));
PolylineOptionspopt=newPolylineOptions().add(latLng1).add(latLng2)
.width(10).color(Color.MAGENTA).pattern(pattern)
.geodesic(true);
mMap.addPolyline(popt);
LatLngBounds.Builderbuilder=newLatLngBounds.Builder();
builder.include(marker1.getPosition());
builder.include(marker2.getPosition());
LatLngBoundsbounds= builder.build();
intpadding=150; // offset from edges of the map in pixelsCameraUpdatecu= CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.moveCamera(cu);
mMap.animateCamera(cu);
}
The resulting polyline is shown in my screenshot
I hope this helps!
Solution 2:
Hy , you need to change your showCurvePolyline method with this method.
privatevoidshowCurvedPolyline(LatLng p1, LatLng p2, double k) {
//Calculate distance and heading between two pointsdoubled=0;
if (d == 0)
d= SphericalUtil.computeDistanceBetween(origin, dest);
doubleh= SphericalUtil.computeHeading(p1, p2);
// Calculate midpoint positionLatLngmidPoint= SphericalUtil.computeOffset(p1, d / 2, h);
//Apply some mathematics to calculate position of the circle centerdoublex= (1-k*k)*d*0.5/(2*k);
doubler= (1+k*k)*d*0.5/(2*k);
LatLngc= SphericalUtil.computeOffset(p, x, h + 90.0);
//Polyline optionsPolylineOptionsoptions=newPolylineOptions();
List<PatternItem> pattern = Arrays.<PatternItem>asList(newDash(30), newGap(20));
//Calculate heading between circle center and two pointsdoubleh1= SphericalUtil.computeHeading(c, p1);
doubleh2= SphericalUtil.computeHeading(c, p2);
// Calculate position of the curve center pointdoublesqrCurvature= DEFAULT_CURVE_ROUTE_CURVATURE * DEFAULT_CURVE_ROUTE_CURVATURE;
doubleextraParam= distance / (4 * DEFAULT_CURVE_ROUTE_CURVATURE);
doublemidPerpendicularLength= (1 - sqrCurvature) * extraParam;
doubler= (1 + sqrCurvature) * extraParam;
LatLngmidCurvePoint= SphericalUtil.computeOffset(midPoint, midPerpendicularLength, heading - 90.0);
// Calculate heading between circle center and two pointsdoubleheadingToOrigin= SphericalUtil.computeHeading(midCurvePoint, origin);
doubleheadingToDest= SphericalUtil.computeHeading(midCurvePoint, dest);
// Calculate positions of points on the curvedoublestep= (headingToDest - headingToOrigin) / DEFAULT_CURVE_POINTS;
List<LatLng> points = newArrayList<>();
for (inti=0; i < DEFAULT_CURVE_POINTS; ++i) {
points.add(SphericalUtil.computeOffset(midCurvePoint, r, headingToOrigin + i * step));
}
for (int i=0; i <points; i++) {
LatLngpi= SphericalUtil.computeOffset(c, r, h1 + i * step);
options.add(pi);
}
//Draw polyline
mMap.addPolyline(options.width(10).color(Color.MAGENTA).geodesic(false).pattern(pattern));
}
then you can draw the arcPolyLine.It was provided in the File Provided by previous answer.
Solution 3:
Here is Route object I am using after some improvement from @xomena
My changes:
- Use
heading - 90.0
instead ofheading + 90.0
- Rename variables, make constants (Still figuring out what is some part of the code is doing...)
- Rewrite with my new logic
Basically, I only draw a curve route if the distance between origin and dest is less than 1000km. For longer distance, as I mentioned in here, I found out the part h1 + i * step
inside the loop make a small error due to double calculation error
in every iterator and make the final route not being placed correctly.
My safe choice is to draw a crow flight route instead, but my suggestion is not to use k = 1 for this, it's not performance efficiency. I wrote another method which just adds origin and dest points to the Route, skips all other complex calculation.
I will try to come back with this long-curve route problem in the future, for now, this solution still suitable for my problem.
EDIT:
Here is my solution for the second problem so far:
- Change
heading + 90
orheading - 90
didn't fix the problem, so don't Instead, change the
step
variable calculation like this:double step = Math.toDegrees(Math.atan(distance / 2 / midPerpendicularLength)) * 2 / DEFAULT_CURVE_POINTS;
Solution 4:
There is another way to draw an arc. Using the google maps projection API to draw the polylines on an overlay view enables us to do a lot of things. Check this repo that has an example.
Post a Comment for "Draw Arc Polyline In Google Map"