Sum*_*man 5 android google-maps
我正在研究地图基础应用程序动画标记.有一个标记从服务器以30秒的间隔更新.标记总是移动到地图的中心,所以我关闭了moveCamera标记,但是当标记移动到地图之外时,标记不会进入地图视图.所以我想在标记从地图视图中移动时相机移动
在设置标记的新位置之前,请检查它的新位置和地图视图的当前边界.
LatLng newPosition = new LatLng(...);
boolean contains = mMap.getProjection()
    .getVisibleRegion()
    .latLngBounds
    .contains(newPosition);
if(!contains){
    // MOVE CAMERA
}
// UPDATE MARKER POSITION
Run Code Online (Sandbox Code Playgroud)
编辑
我创建了一个样本路线,定期模拟地图上的每个点.路线要点
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),
            ...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
然后我在样本活动中创建一个方法,计算当前区域的边界和标记在该区域上的X,Y点.活动要点
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);
}
Run Code Online (Sandbox Code Playgroud)
然后开始模拟积分
mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        moveCamera(mPoints.get(mCurrentPos));
        if(++mCurrentPos < mPoints.size()){
            mHandler.postDelayed(this, 1500);
        }
    }
}, 1500);
Run Code Online (Sandbox Code Playgroud)
我试过,它在我身上很好用
所以,如果我理解正确,它也适合你,那么我可以解释.
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           2330 次  |  
        
|   最近记录:  |