当标记移出屏幕时如何移动相机?

时间:2016-05-24 08:14:47

标签: android google-maps

我正在使用地图基础应用程序来设置动画标记。有一个标记从服务器以30秒的间隔更新。标记总是移动到地图的中心,所以我关闭了moveCamera标记,但是当标记移动到地图之外时,标记不会进入地图视图。所以我想在标记从地图视图中移动时移动相机

3 个答案:

答案 0 :(得分:9)

在设置标记的新位置之前,请检查它的新位置和地图视图的当前边界。

LatLng newPosition = new LatLng(...);

boolean contains = mMap.getProjection()
    .getVisibleRegion()
    .latLngBounds
    .contains(newPosition);

if(!contains){
    // MOVE CAMERA
}
// UPDATE MARKER POSITION
  • 在标记的地图视图中的每个新位置期间,标记移动(不是 地图中心)
  • 只是如果下一个位置不在视野范围内,相机和标记将居中。

修改

我创建了一个样本路线,定期模拟地图上的每个点。 Route gist

public class SampleRoute {
    public static List<LatLng> GetPoints() {
        return new ArrayList<>(Arrays.asList(
            new LatLng(38.4670419, 27.1647131),
            new LatLng(38.4667244, 27.1648277),
            new LatLng(38.4666633, 27.1649079),
            new LatLng(38.4665983, 27.1648022),
            new LatLng(38.4665958, 27.1647843),
            new LatLng(38.4665958, 27.1647843),
            new LatLng(38.4665809, 27.1646429),
            new LatLng(38.4665704, 27.1645506),
            new LatLng(38.4665529, 27.1644067),
            ...
        }
    }
}

然后我在样本活动中创建一个方法,计算当前区域的边界和标记在该区域上的X,Y点。 Activity gist

private void moveCamera(LatLng destination){
    Projection projection =  mMap.getProjection();

    LatLngBounds bounds = projection.getVisibleRegion().latLngBounds;
    int boundsTopY = projection.toScreenLocation(bounds.northeast).y;
    int boundsBottomY = projection.toScreenLocation(bounds.southwest).y;
    int boundsTopX = projection.toScreenLocation(bounds.northeast).x;
    int boundsBottomX = projection.toScreenLocation(bounds.southwest).x;

    int offsetY = (boundsBottomY - boundsTopY) / 10;
    int offsetX = (boundsTopX - boundsBottomX ) / 10;

    Point destinationPoint = projection.toScreenLocation(destination);
    int destinationX = destinationPoint.x;
    int destinationY = destinationPoint.y;

    int scrollX = 0;
    int scrollY = 0;

    if(destinationY <= (boundsTopY + offsetY)){
        scrollY = -(Math.abs((boundsTopY + offsetY) - destinationY));
    }
    else if(destinationY >= (boundsBottomY - offsetY)){
        scrollY = (Math.abs(destinationY - (boundsBottomY - offsetY)));
    }
    if(destinationX >= (boundsTopX - offsetX)){
        scrollX = (Math.abs(destinationX - (boundsTopX - offsetX)));
    }
    else if(destinationX <= (boundsBottomX + offsetX)){
        scrollX = -(Math.abs((boundsBottomX + offsetX) - destinationX));
    }
    mMap.animateCamera(CameraUpdateFactory.scrollBy(scrollX, scrollY));
    mMarker.setPosition(destination);
}

然后开始模拟点

mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        moveCamera(mPoints.get(mCurrentPos));
        if(++mCurrentPos < mPoints.size()){
            mHandler.postDelayed(this, 1500);
        }
    }
}, 1500);

我试过,它在我身上运作良好

所以,如果我理解正确并且它也适合你,那么我可以解释一下。

答案 1 :(得分:1)

更新标记位置后,您可以将相机设置为标记位置的动画。以下示例代码可以帮助您

  LatLng definedLoc = new LatLng(latitudeValue, longitudeValue);
  CameraPosition cameraPosition = new CameraPosition.Builder().target(definedLoc).zoom(13.0F).build();
  map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

答案 2 :(得分:1)

如果地图边界不包含使用GoogleMap.OnCameraChangeListener的标记,则可以将相机设置为标记位置的动画:

private Marker marker;

// ...

@Override
public void onCameraChange(final CameraPosition cameraPosition) {
    ensureMarkerOnBounds();
}

private void ensureMarkerOnBounds() {
    if (marker != null) {
        if (!mMap.getProjection().getVisibleRegion().latLngBounds.contains(marker.getPosition())) {
            mMap.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
        }
    }
}

// This is the function that you use to move your marker
private void moveMarker (Marker marker) {
    // ... Your code to move your marker
    ensureMarkerOnBounds();
}