首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得谷歌热图点击监听?

如何获得谷歌热图点击监听?
EN

Stack Overflow用户
提问于 2019-06-20 12:59:29
回答 1查看 460关注 0票数 2

我能够正确地显示热图

代码语言:javascript
复制
// 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+

有没有什么方法可以让我获得添加热图的点击事件?

EN

回答 1

Stack Overflow用户

发布于 2019-06-22 23:38:44

热图只是没有OnClickListener接口的TileOverlay。所以,如果你只需要热图单击点的颜色,你可以在GoogleMap.onMapClickListener()中这样做:

代码语言:javascript
复制
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);

        }

    }
});

哪里

代码语言:javascript
复制
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))));
}

但是如果你不需要颜色,而是数值,你应该根据颜色计算整数,或者创建CustomTile类,不仅存储x,y和位图data,而且存储包含计算出的热图数值的数组。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56679230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档