根据LatLngBounds,GoogleMap moveCamera或animateCamera不准确

时间:2013-07-28 15:24:44

标签: android google-maps google-maps-android-api-2

我对使用CameraUpdateFactory.newLatLngBounds()的moveCamera和animateCamera的准确性有所怀疑。我将创建和发送作为参数的LatLngBounds对象与moveCamera / animateCamera(CameraUpdateFactory.newLatLngBounds()),onCameraChange()事件中的 map .getProjection()。getVisibleRegion()。latLngBounds进行比较GoogleMap.CancelableCallback#onFinish()。 他们不匹配。 有人遇到过这个问题吗?这是一个错误吗?

我的代码是:

final LatLngBounds boundingBox = MapUtils.getBoundingBox(mCurrLocation.getLatitude(), mCurrLocation.getLongitude(), mCurrRadius);

try {
    if (animate) {
        map.animateCamera(CameraUpdateFactory.newLatLngBounds(boundingBox, 0),
            new GoogleMap.CancelableCallback() {
                @Override
                public void onFinish() {
                    if (!boundingBox.equals(map.getProjection().getVisibleRegion().latLngBounds)) {
                            map.moveCamera(CameraUpdateFactory.newLatLngBounds(boundingBox, 0));
                        }
                    }

                    @Override
                    public void onCancel() {
                    }
                });
    } else {
        map.moveCamera(CameraUpdateFactory.newLatLngBounds(boundingBox, 0));
    }
}

请忽略我在这里比较2个对象的方式(等于)。我还调试了代码并检查了2个对象并发现它们不匹配 -

moveCamera:

  

boundingBox -

     
    

LatLngBounds {southwest = lat / lng:(32.08455383290544,34.773394063736845),northeast = lat / lng:(32.09730731777191,34.788375176773286)}

  
     

map.getProjection()。getVisibleRegion()。latLngBounds -

     
    

LatLngBounds {southwest = lat / lng:(32.084496299473756,34.77339383214712),northeast = lat / lng:(32.09736452396455,34.78837497532368)}

  

animateCamera:

  

boundingBox -

     
    

LatLngBounds {southwest = lat / lng:(40.70798493778415,-74.01434069136418),northeast = lat / lng:(40.72072004852845,-73.99760391411343)}

  
     

map.getProjection()。getVisibleRegion()。latLngBounds -

     
    

LatLngBounds {southwest = lat / lng:(40.70798500292429,-74.01539381593466),northeast = lat / lng:(40.72071968970514,-73.99655096232891)}

  

2 个答案:

答案 0 :(得分:0)

这似乎是两个bug && !bug的组合。

有关错误说明,请参阅:http://code.google.com/p/gmaps-api-issues/issues/detail?id=5353

基本上,你会在小数点后第5或第6位有差异。

另一个问题是,当您将LatLngBounds发送到其中一个功能时,生成的LatLngBounds将是新的,完全适合屏幕,而原始内容将在其中。

答案 1 :(得分:0)

animateCamera和LatLngBounds存在错误。该方法不会将相机准确移动到预期的框。我怎么知道的?我通过添加以下代码验证了这一点:

private LatLngBounds mBoundingBox;
private boolean mCameraAnimated;

public void setCurrentLocation(Location currentLocation, String radius, boolean animate) {
    mCurrLocation = currentLocation;
    mCurrRadius = MapUtils.getRadiusDecimalValue(radius);
    mBoundingBox = MapUtils.getBoundingBox(mCurrLocation.getLatitude(), mCurrLocation.getLongitude(), mCurrRadius);
    mCameraAnimated = animate;

    if (animate) {
            mEventMap.animateCamera(CameraUpdateFactory.newLatLngBounds(mBoundingBox, 0));
    } else {
            mEventMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mBoundingBox, 0));
    }
}

@Override
public void onCameraChange(CameraPosition cameraPosition) {
    // This ugly solution was added because there is a bug with camera animate which doesn't move to the correct bounding box.
    if (mBoundingBox != null && !mBoundingBox.equals(mEventMap.getProjection().getVisibleRegion().latLngBounds)) {
        LatLngBounds originalBoundingBox = mBoundingBox;
        mBoundingBox = null;
        if (mCameraAnimated) {
            mCameraAnimated = false;
            mEventMap.moveCamera(CameraUpdateFactory.newLatLngBounds(originalBoundingBox, 0));
            return;
        }
    } ...

现在,在额外的moveCamera之后,相机正确定位 新问题是有时onCameraChange没有被这个额外的moveCamera触发,因此我没有执行我的实际onCameraChange代码(用'...'标记)。