我正在构建一个应用,我在onCameraIdle
中从API加载数据。该请求需要目标位置和半径,以便为我提供结果。
我用来从当前相机获取半径的代码是:
/**
* @param map The Map on which to compute the radius.
* @return The circle radius of the area visible on map, in meters.
*/
public float getRadiusVisibleOnMap(GoogleMap map) {
VisibleRegion visibleRegion = map.getProjection().getVisibleRegion();
LatLng farRight = visibleRegion.farRight;
LatLng farLeft = visibleRegion.farLeft;
LatLng nearRight = visibleRegion.nearRight;
LatLng nearLeft = visibleRegion.nearLeft;
float[] distanceWidth = new float[1];
Location.distanceBetween(
(farRight.latitude + nearRight.latitude) / 2,
(farRight.longitude + nearRight.longitude) / 2,
(farLeft.latitude + nearLeft.latitude) / 2,
(farLeft.longitude + nearLeft.longitude) / 2,
distanceWidth
);
float[] distanceHeight = new float[1];
Location.distanceBetween(
(farRight.latitude + nearRight.latitude) / 2,
(farRight.longitude + nearRight.longitude) / 2,
(farLeft.latitude + nearLeft.latitude) / 2,
(farLeft.longitude + nearLeft.longitude) / 2,
distanceHeight
);
float radius;
if (distanceWidth[0] > distanceHeight[0]) {
radius = distanceWidth[0];
} else {
radius = distanceHeight[0];
}
return radius;
}
我是从here
得到的在某些情况下我只需要知道缩放级别,因此当缩放动画完成时,信息已经加载到地图上。
我尝试从here调整代码以获得知道缩放级别的半径,而不是相反。
public int getZoomLevel(Circle circle) {
if (circle != null) {
double radius = circle.getRadius();
double scale = radius / 500;
zoomLevel = (int) (16 - Math.log(scale) / Math.log(2));
}
return zoomLevel;
}
我得到了公式:
公式不正确。它没有输出正确的尺寸。对于14的缩放级别,我使用相机的VisibleRegion得到半径尺寸为2.697331km。如果我使用公式计算半径,结果是0.008km。
答案 0 :(得分:0)
看起来最佳做法是找到适合内部地图矩形的最小半径值。
你几乎就在那里,使用下面的修改函数,使用Pythagorean theorem来找到包含地图“矩形”所需的最小半径。
@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
googleMap.setOnMarkerClickListener(this);
googleMap.setOnCameraIdleListener(() -> {
CameraPosition cameraPosition = googleMap.getCameraPosition();
double radiusInMeters = getMapVisibleRadius();
double radiusInKilometers = radiusInMeters / 1000;
double latitude = cameraPosition.target.latitude;
double longitude = cameraPosition.target.longitude;
// TODO you take it from here,
// I use firebase to get relevant markers based on the lat,long,radius
});
}
private double getMapVisibleRadius() {
VisibleRegion visibleRegion = googleMap.getProjection().getVisibleRegion();
float[] distanceWidth = new float[1];
float[] distanceHeight = new float[1];
LatLng farRight = visibleRegion.farRight;
LatLng farLeft = visibleRegion.farLeft;
LatLng nearRight = visibleRegion.nearRight;
LatLng nearLeft = visibleRegion.nearLeft;
Location.distanceBetween(
(farLeft.latitude + nearLeft.latitude) / 2,
farLeft.longitude,
(farRight.latitude + nearRight.latitude) / 2,
farRight.longitude,
distanceWidth
);
Location.distanceBetween(
farRight.latitude,
(farRight.longitude + farLeft.longitude) / 2,
nearRight.latitude,
(nearRight.longitude + nearLeft.longitude) / 2,
distanceHeight
);
double radiusInMeters = Math.sqrt(Math.pow(distanceWidth[0], 2) + Math.pow(distanceHeight[0], 2)) / 2;
return radiusInMeters;
}