gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:04:32 25 4
gpt4 key购买 nike

我正在开发 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com