Как получить прослушиватель кликов Google Heat Map? - PullRequest
2 голосов
/ 20 июня 2019

Я могу правильно отобразить карту тепла

// 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 голосов
/ 22 июня 2019

Тепловая карта - это просто TileOverlay без интерфейса 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, но и массива с вычисленными числовыми значениями для теплового отображения.

...