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

标签 android google-maps

我正在开发 map 基础应用程序来为标记设置动画。有一个标记从服务器每隔 30 秒更新一次。标记总是移动到 map 的中心,所以我关闭了 moveCamera 标记,但是当标记移到 map 之外时,标记不会出现在 map View 中。所以我想在标记离开 map View 时移动相机

最佳答案

在设置标记的新位置之前,检查它的新位置和 map View 的当前边界。

LatLng newPosition = new LatLng(...);

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

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

编辑

我创建了一条示例路线来定期模拟 map 上的每个点。 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),
            ...
        }
    }
}

然后我在示例 Activity 中创建了一个方法,用于计算当前区域的边界和该区域上标记的 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);

我试过了,效果很好

所以,如果我对您的理解正确并且它也适用于您,那么我可以解释一下。

关于android - 当标记超出屏幕时如何移动相机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37407902/

相关文章:

java - 使用包裹发送未知数据类型的对象时出现问题

java - 不兼容的 fragment 类型

android - 如何在后台运行计时器?

android - 如何从url获取JSON数据?

javascript - Android:使用 Javascript 制作全景图像的循环 Horizo​​ntalScrollView

java - 安卓、谷歌地图 : how to pass address to Res

javascript - 使用 Google map 绘制 svg 圆圈

android - 谷歌地图获取方向搜索算法

android - 如何从谷歌地图应用程序获取路线或目的地?

javascript - 谷歌地图 API V3 中的 tohere 和 fromhere 消息窗口未定义