Android 将热图连接到当前位置

标签 android google-maps heatmap

我试图在教程的帮助下使用“谷歌地图”在 Android 设备上绘制热图 here在 Google 开发人员中,使用 Android utility library

使用教程中描述的示例代码,我实现了如下热图:

enter image description here

如您所见,热图位置显示在绿色圆圈和点中,但实际上,我想实现如下所示:

enter image description here

那是通过一条线将热图连接到当前位置。

有什么想法或帮助吗?

最佳答案

您可以使用 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));
}

您会在源点之间获得平滑的“热图线”,就像这样:

Heat map Path

您可以通过HeatmapTileProvider 参数调整“热图线”的颜色和宽度。如果你只在路上需要折线,你可以使用 Snap to Road的一部分 Google Maps Roads APIWaleed Asim 中提供的示例评论。

关于Android 将热图连接到当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51704211/

相关文章:

java - 使用 android Facebook API(版本 3.0+)更新我的 Facebook 状态

c# - 如何在谷歌地图上添加路线方向?

google-maps - 让所有街道在 Google map 视口(viewport)中可见

python - 更改 Seaborn 热图中刻度标签的旋转

android - 如何为 ACTION_MOVE 获得更快的触摸频率

android - 没有单选按钮的 AlertDialog setSingleChoice

Android 模拟器没有响应 - 卡住

java - Android 谷歌地图 API 无法正常工作

python - 在 Matplotlib/Cartopy 中制作颜色条图例

r - R 中的真实热图