首页 > 解决方案 > How to draw smooth(lines connected without any breaks) lines over google map in Android?

问题描述

I am building a path tracing application on Android. This particular activity will receive position and whenever I get a position, I will join the last point with the current point. With this, I am getting the path but it is not smooth at joints.

A is something I am getting. B is what I am trying to achieve.

A

   ________________
  |________________
|  |
|  |

B

 ________________
|________________
|  |
|  |

The problem is because I draw lines with 2 points. When google draws 3 points, the joint is connected properly. I am looking for a way something other than drawing 3 points instead of 2.

I am adding points to polyline

PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.add(lastPosition);
polylineOptions.add(currentPosition);
polylineOptions.color(Color.BLUE);
polylineOptions.width(8);

mMap.addPolyline(polylineOptions);

I also tried to set polylineOptions.jointType(JointType.ROUND) but not much improvement.

标签: androidgoogle-maps

解决方案


JointType影响一条折线的线段,而不是单独的折线。所以不要用两点线绘制路径 - 将整个路径绘制为一条折线。像这样的东西:

// add polyline to map (once) and save reference to it in polyLine variable
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.add(lastPosition);
polylineOptions.add(currentPosition);
polylineOptions.color(Color.BLUE);
polylineOptions.width(8);
Polyline polyLine = mMap.addPolyline(polylineOptions);

...
// every time when currentPosition updated
List<LatLng> polyLinePoints = polyLine.getPoints();   // get polyline points
polyLinePoints.add(currentPosition);                  // add currentPosition to polyline points
polyLine.setPoints(polyLinePoints);                   // update polyline points

推荐阅读