简体   繁体   中英

How to get google heat map click listener?

I am able to display heat map properly

// 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));

i have used this lib compile 'com.google.maps.android:android-maps-utils:0.5+ '

is there anyway i can get click events for added heat map?

Heat Map is just TileOverlay with no OnClickListener interface. So, if you need just color of heat Map clicked point, you can do that in GoogleMap.onMapClickListener() this way:

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

        }

    }
});

where

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

But if you need not color, but numeric value, you should calculate int based on color, or create CustomTile class for store not only x,y and bitmap data , but array with calculated numeric values for heatmap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM