Java 按缩放级别更改半径
我希望根据缩放级别以米或公里为单位计算半径 我发现了这个公式——它给出了粗略计算的每像素米数: 此外,这些链接有助于我准确地理解我想要实现的目标: 这就是实现:Java 按缩放级别更改半径,java,android,google-maps,google-maps-android-api-2,Java,Android,Google Maps,Google Maps Android Api 2,我希望根据缩放级别以米或公里为单位计算半径 我发现了这个公式——它给出了粗略计算的每像素米数: 此外,这些链接有助于我准确地理解我想要实现的目标: 这就是实现: double calculateScale(float zoomLevel, LatLng centerPos){ double meters_per_pixel = 156543.03392 * Math.cos(centerPos.latitude * Math.PI / 180) / Math.pow(2, zoomL
double calculateScale(float zoomLevel, LatLng centerPos){
double meters_per_pixel = 156543.03392 * Math.cos(centerPos.latitude * Math.PI / 180) / Math.pow(2, zoomLevel);
return meters_per_pixel;
}
这就是我如何听到缩放级别的变化:
_map.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
@Override
public void onCameraIdle() {
Log.d(Consts.TAGS.FRAG_MAIN_MAP,"Current Zoom : " + _map.getCameraPosition().zoom);
Log.d(Consts.TAGS.FRAG_MAIN_MAP,"Center Lat : " + _map.getCameraPosition().target.latitude +
", Center Long : " + _map.getCameraPosition().target.longitude);
}}
现在,每当用户更改缩放级别时,我希望确定地图视图中可以显示的最大半径是多少…您可以使用下一个片段:
_map.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
@Override
public void onCameraIdle() {
Log.d(Consts.TAGS.FRAG_MAIN_MAP,"Current Zoom : " + _map.getCameraPosition().zoom);
Log.d(Consts.TAGS.FRAG_MAIN_MAP,"Center Lat : " + _map.getCameraPosition().target.latitude +
", Center Long : " + _map.getCameraPosition().target.longitude);
float zoom = _map.getCameraPosition().zoom;
LatLng position = _map.getCameraPosition().target;
double maxRadius = calculateScale(zoom, position) * mapViewDiagonal() / 2;
}
}
private mapViewDiagonal() {
return Math.sqrt(_map.getWidth() * _map.getWidth() + _map.getHeight() * _map.getHeight());
}
您可以使用下一个代码段:
_map.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
@Override
public void onCameraIdle() {
Log.d(Consts.TAGS.FRAG_MAIN_MAP,"Current Zoom : " + _map.getCameraPosition().zoom);
Log.d(Consts.TAGS.FRAG_MAIN_MAP,"Center Lat : " + _map.getCameraPosition().target.latitude +
", Center Long : " + _map.getCameraPosition().target.longitude);
float zoom = _map.getCameraPosition().zoom;
LatLng position = _map.getCameraPosition().target;
double maxRadius = calculateScale(zoom, position) * mapViewDiagonal() / 2;
}
}
private mapViewDiagonal() {
return Math.sqrt(_map.getWidth() * _map.getWidth() + _map.getHeight() * _map.getHeight());
}
这个答案向我展示了如何以适合我的用例的方式计算最大半径(我使用了地图容器布局而不是_map)。对不起,我使用了太多的细节。主要目的是得到视图对角线的一半,它将是maxRadius.:)很高兴你明白了这一点。这个答案告诉我如何以适合我的用例的方式计算最大半径(我使用了地图容器布局而不是地图)。对不起,我使用了太多的细节。主要目的是得到视图对角线的一半,它将是maxRadius.:)很高兴你明白了这一点。