如何获取谷歌热图点击监听器?

时间:2019-06-20 04:59:28

标签: android google-maps heatmap

我能够正确显示热图

// Create a heat map tile provider, passing it the latlngs of the police stations.
    mProvider = new HeatmapTileProvider.Builder()
        .data(list)
        .build();
    // Add a tile overlay to the map, using the heat map tile provider.
    mOverlay = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(mProvider));

我已经使用了这个库compile 'com.google.maps.android:android-maps-utils:0.5+'

无论如何,我是否可以获得点击事件以添加热图?

1 个答案:

答案 0 :(得分:0)

热图只是@global,没有OnClickListener界面。因此,如果您只需要热图点击点的颜色,则可以通过以下方式在GoogleMap.onMapClickListener()中进行操作:

mGoogleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng latLng) {
        // get current zoom
        int zoom = (int)mGoogleMap.getCameraPosition().zoom;

        // get tile top-left coordinates in tile coordinate system
        int tileX = getTileX(latLng, zoom);
        int tileY = getTileY(latLng, zoom);

        // get Tile object (with heatmap color data) from tile provider 
        Tile tile = mProvider.getTile(tileX, tileY, zoom);
        if (tile.data == null) {
            return;
        }

        // decode heatmap data into bitmap
        Bitmap bitmap = BitmapFactory.decodeByteArray(tile.data, 0, tile.data.length);

        // get tile coordinates in pixels
        LatLng tileNorthWest = new LatLng(tile2lat(tileY, zoom), tile2long(tileX, zoom));
        long tileNorthWestX = lonToX(tileNorthWest.longitude, zoom);
        long tileNorthWestY = latToY(tileNorthWest.latitude, zoom);

        // get "click" point coordinates in pixels
        long pointNorthWestX = lonToX(latLng.longitude, zoom);
        long pointNorthWestY = latToY(latLng.latitude, zoom);

        // calculate offset of "click" point within current tile
        // x2 because of hi density tiles 512x512
        long dx = 2 * (pointNorthWestX - tileNorthWestX);
        long dy = 2 * (pointNorthWestY - tileNorthWestY);

        // test calculated coordinates and get color of clicked point as Heat Map data
        if (dx >= 0 && dx < bitmap.getWidth() && dy >= 0 && dy < bitmap.getHeight()) {
            // dx, dy - coordinates of current tile of target heatmap 
            // pixelColor is color value of target heatmap 
            int pixelColor = bitmap.getPixel((int) dx, (int) dy);

        }

    }
});

其中

public static int getTileX(final LatLng latLng, final int zoom) {
    int tileX = (int)Math.floor((latLng.longitude + 180) / 360 * (1 << zoom));
    if (tileX < 0)
        tileX = 0;
    if (tileX >= (1 << zoom))
        tileX = (1 << zoom)-1;
    return tileX;
}

public static int getTileY(final LatLng latLng, final int zoom) {
    int tileY = (int) Math.floor( (1 - Math.log(Math.tan(Math.toRadians(latLng.latitude)) + 1 / Math.cos(Math.toRadians(latLng.latitude))) / Math.PI) / 2 * (1 << zoom) ) ;
    if (tileY < 0)
        tileY = 0;
    if (tileY >= (1 << zoom))
        tileY=((1 << zoom)-1);
    return tileY;
}

public static long lonToX(double lon, int zoom) {
    int offset = 256 << (zoom - 1);
    return (int)Math.floor(offset + (offset * lon / 180));
}

public static long latToY(double lat, int zoom) {
    int offset = 256 << (zoom - 1);
    return (int)Math.floor(offset - offset / Math.PI * Math.log((1 + Math.sin(Math.toRadians(lat)))
            / (1 - Math.sin(Math.toRadians(lat)))) / 2);
}

public static double tile2long(int x, int zoom) {
    return (x / Math.pow(2,zoom) * 360 - 180);
}

public static double tile2lat(int y, int zoom) {
    double n = Math.PI - 2 * Math.PI * y /Math.pow(2,zoom);
    return (180 / Math.PI * Math.atan(0.5 * (Math.exp(n)-Math.exp(-n))));
}

但是,如果您不需要颜色,而是数值,则应基于颜色计算int,或创建CustomTile类以存储x,y和位图data,并存储计算出的热图数值。