我试图在教程的帮助下使用“谷歌地图”在 Android 设备上绘制热图 here在 Google 开发人员中,使用 Android utility library
使用教程中描述的示例代码,我实现了如下热图:
如您所见,热图位置显示在绿色圆圈和点中,但实际上,我想实现如下所示:
那是通过一条线将热图连接到当前位置。
有什么想法或帮助吗?
最佳答案
您可以使用 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));
}
您会在源点之间获得平滑的“热图线”,就像这样:
您可以通过HeatmapTileProvider
参数调整“热图线”的颜色和宽度。如果你只在路上需要折线,你可以使用 Snap to Road的一部分 Google Maps Roads API或 Waleed Asim 中提供的示例评论。
关于Android 将热图连接到当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51704211/