Android 将热图连接到当前位置

Android connecting Heatmaps to current location

我试图在 Android 设备上使用 "Google Map" 在教程的帮助下绘制热图 here on Google developers, using Android utility library

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

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

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

有什么想法或帮助吗?

您可以使用 SphericalUtil.interpolate() from Google Maps Android API Utility Library 在您的源点之间获取额外的点并将其发送到 HeatmapTileProvider 以获得 "heat map line"。例如,使用此代码:

@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));
}

你在源点之间变得平滑 "heatmap line",就像这样:

您可以通过HeatmapTileProvider参数调整"heatmap line"的颜色和宽度。如果你只在路上需要折线,你可以使用 Snap to Road part of Google Maps Roads API or examples provided in Waleed Asim 评论。