代码之家  ›  专栏  ›  技术社区  ›  Tejashwi Kalp Taru

Android将热图连接到当前位置

  •  1
  • Tejashwi Kalp Taru  · 技术社区  · 6 年前

    我试着用“谷歌地图”在Android设备上绘制一个热图,这是在教程的帮助下完成的 here 关于Google开发者,使用 Android utility library

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

    enter image description here

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

    enter image description here

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

    有什么想法或帮助吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Andrii Omelchenko    6 年前

    你可以用 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

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