谷歌地图两指捏/拉伸回调

时间:2016-10-19 00:15:43

标签: android-maps-v2 supportmapfragment android-gesture

在我的地图中,我尝试使用ScaleGestureDetector捕捉放大/缩小,但我从不接收onScaleonScaleBegin的任何回调或onScaleEnd

在我的片段onCreateView中,我初始化:

scaleGestureDetector = new ScaleGestureDetector(getContext(), new simpleOnScaleGestureListener());

我实现了这样的回调:

public class simpleOnScaleGestureListener extends
        SimpleOnScaleGestureListener {

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        // TODO Auto-generated method stub
        startScale = detector.getScaleFactor();
        Log.d(TAG, "::onScale:: " + detector.getScaleFactor());
        return true;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        // TODO Auto-generated method stub
        Log.d(TAG, "::onScaleBegin:: " + detector.getScaleFactor());
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {
        // TODO Auto-generated method stub
        Log.d(TAG, "::onScaleEnd:: " + detector.getScaleFactor());
        endScale = detector.getScaleFactor();

}

此外,假设每当用户放大/缩小时都会连续调用回调,这是公平的吗?

1 个答案:

答案 0 :(得分:0)

我能够解决获得回调的问题。基本上,有两件事:

  1. 在您的活动/片段中,实施GoogleMap.OnCameraIdleListener
  2. onMapReady()call mMap.setOnCameraIdleListener(this);
  3. 因此,覆盖onCameraIdle()
  4. @Override public void onCameraIdle() { Log.i(TAG, "::onCameraIdle::" + mMap.getCameraPosition().toString()); }

    获得纬度/长度,缩放,倾斜和方位,基本上是CameraPosition

    我找到了一种方法,通过参考这个response

    获得以米为单位的半径
    VisibleRegion vr = map.getProjection().getVisibleRegion();
    
    Location center = new Location("center");
    center.setLatitude(vr.latLngBounds.getCenter().latitude);
    center.setLongitude(vr.latLngBounds.getCenter().longitude);
    
    //Location("radiusLatLng") as mentioned in google maps sample
    Location farVisiblePoint = new Location("radiusLatLng");
    farVisiblePoint.setLatitude(vr.farLeft.latitude);
    farVisiblePoint.setLongitude(vr.farLeft.longitude);
    
    radius = center.distanceTo(farVisiblePoint);