4

我对使用 CameraUpdateFactory.newLatLngBounds() 的 moveCamera 和 animateCamera 的准确性有一些疑问。我比较了一个 LatLngBounds 对象,我创建并作为参数发送到 moveCamera/animateCamera (CameraUpdateFactory.newLatLngBounds()),映射.getProjection().getVisibleRegion().latLngBounds 在 onCameraChange() 事件甚至 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 个对象,发现它们不匹配 -

移动相机:

边界框 -

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

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

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

动画相机:

边界框 -

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

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

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

4

2 回答 2

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 代码(标记为'...')。

于 2013-08-07T12:38:18.027 回答
0

这似乎是两者的结合bug && !bug

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

基本上你会有第 5 位或第 6 位小数的差异。

另一个问题是,当您发送LatLngBounds到其中一个功能时,结果LatLngBounds将是完全适合屏幕的新结果,而您的原件将在其中。

于 2013-07-28T19:31:15.993 回答