我正在尝试检测Google地图中的缩放级别。我的地图位于片段内,该片段扩展了另一个片段。
我在班上实现了OnCameraChangeListener。
这是我为检测缩放级别更改而编写的代码:
private float currentZoom = -1;
public GoogleMap.OnCameraChangeListener getCameraChangeListener()
{
return new GoogleMap.OnCameraChangeListener()
{
@Override
public void onCameraChange(CameraPosition position)
{
if(currentZoom != position.zoom)
{
currentZoom = position.zoom;
Context context = mContext;
CharSequence text = Float.toString(currentZoom);
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
};
}
// This is the Method that was created after Implementing the OnCameraChangeListener
@Override
public void onCameraChange(CameraPosition cameraPosition) {
getCameraChangeListener();
}
当我在手机中打开应用程序并尝试放大或缩小时,没有任何反应。通常在阅读其他有关缩放级别检测的问题的答案后,这就是我想出的代码。但它没有用。
请让我知道如何让它发挥作用。非常感谢。
答案 0 :(得分:1)
onCameraChangeListener将在GoogleMap对象的实例上设置。更多详情请见http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html
答案 1 :(得分:1)
您需要将GoogleMap.OnCameraChangeListener
分配给您的GoogleMap实例。下面是一个示例代码,可以帮助您找到实现方法:
public void prepareMap(){
mMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
setupMap(googleMap);
}
});
}
private void setupMap(GoogleMap googleMap){
mMap = googleMap;
mMap.setOnCameraChangeListener(getCameraChangeListener());
}
public GoogleMap.OnCameraChangeListener getCameraChangeListener() {
return new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition position) {
if(currentZoom != position.zoom) {
currentZoom = position.zoom;
Context context = mContext;
CharSequence text = Float.toString(currentZoom);
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
};
}