gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:20:09 31 4
gpt4 key购买 nike

我能够正确显示热图

// 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+'

无论如何我可以获得添加的热图的点击事件吗?

最佳答案

热图只是 TileOverlay没有 OnClickListener 接口(interface)。因此,如果您只需要热图点击点的颜色,您可以在 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和bitmap data,但是包含热图计算数值的数组。

关于android - 如何获取谷歌热图点击监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56679230/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com