你可以用
SphericalUtil.interpolate()
从
Google Maps Android API Utility Library
获取源点之间的附加点并将其发送到
HeatmapTileProvider
得到“热图线”。例如,使用此代码:
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
// Create the gradient.
int[] colors = {
Color.rgb(102, 225, 0), // green
Color.rgb(255, 0, 0) // red
};
float[] startPoints = {
0.2f, 1f
};
Gradient gradient = new Gradient(colors, startPoints);
final List<LatLng> sourcePolyPoints = new ArrayList<>();
sourcePolyPoints.add(new LatLng(28.537266, 77.208099));
sourcePolyPoints.add(new LatLng(28.536965, 77.209571));
sourcePolyPoints.add(new LatLng(28.536786, 77.209989));
sourcePolyPoints.add(new LatLng(28.537886, 77.210205));
sourcePolyPoints.add(new LatLng(28.537886, 77.210205));
final List<LatLng> interpolatedPolyPoints = new ArrayList<>();
for (int ixPoint = 0; ixPoint < sourcePolyPoints.size() - 1; ixPoint++) {
int nInterpolated = 50;
for (int ixInterpolated = 0; ixInterpolated < nInterpolated; ixInterpolated++) {
LatLng interpolatedPoint = SphericalUtil.interpolate(sourcePolyPoints.get(ixPoint),
sourcePolyPoints.get(ixPoint + 1), (double) ixInterpolated / (double) nInterpolated);
interpolatedPolyPoints.add(interpolatedPoint);
}
}
// Create the tile provider.
mProvider = new HeatmapTileProvider.Builder()
.data(interpolatedPolyPoints)
.gradient(gradient)
.build();
// Add the tile overlay to the map.
mOverlay = mGoogleMap.addTileOverlay(new TileOverlayOptions().tileProvider(mProvider));
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(28.536965, 77.209571), 16));
}
在源点之间获得平滑的“热图线”,如:
您可以通过以下方式调整“热图线”的颜色和宽度
热图提取器
参数。如果你只在路上需要折线,你可以使用
Snap to Road
部分
Google Maps Roads API
或提供的示例
Waleed Asim
评论。