谷歌地图相机位置不变

时间:2016-05-26 08:34:51

标签: android google-maps

这不是重复的 Android map v2 zoom to show all the markers

我尝试了上面链接中发布的答案,但它对我不起作用,这就是我提出问题的原因

我在谷歌地图上显示两个标记。第一次加载地图并设置地图时,缩放设置正确。但是当标记位置发生变化时,缩放级别不会改变。

这是我的代码。我希望缩放级别在标记位置更改时更改,并且我想以包含两个标记的方式缩放地图

        SubscriberMarker = map.addMarker(new MarkerOptions().position(new LatLng(SUBSCRIBER_LAT, SUBSCRIBER_LNG)).title("Home Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.home)));
        builder.include(SubscriberMarker.getPosition());

        _MyBound = builder.build();



// this part is called when the location is changed: 

                if (FTMarker!=null)
                {
                    FTMarker.remove();
                }

                FTMarker = googleMap.addMarker(new MarkerOptions().position(new LatLng(LAT,LNG)).title("FT Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.woker)));

                builder.include(FTMarker.getPosition());

                _MyBound = builder.build();




// the zoom function is called after that
    public void zoom()
    {

        if (mapView!=null) {
            int padding_percent = 20;
            int padding = Math.min(mapView.getHeight(), mapView.getWidth()) * padding_percent / 100;
            googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(_MyBound, mapView.getWidth(), mapView.getHeight(), padding));
        }
    }

1 个答案:

答案 0 :(得分:0)

添加标记后,如果要为其设置动画,请执行以下操作

LatLng toPosition=new LatLng(dummy_lat,dummy_lon);

MarkerOptions markerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.woker));//set marker custom icon if needed
markerOptions.position(toPosition);//set marker position
markerOptions.title("Marker Title");//set title for marker
mGoogleMap.addMarker(markerOptions);//add marker to map

mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(toPosition, 18.0f));//animate to the marker location

可以通过

找到根据半径的缩放级别
double circleRad = dummy_radius*1000;//multiply by 1000 to make units in KM

Dummy_radius应根据您的第一个LatLon和第二个LatLon计算

private int getZoomLevel(double radius){
            double scale = radius / 500;
            return ((int) (16 - Math.log(scale) / Math.log(2)));
}

float zoomLevel = getZoomLevel(circleRad);
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLon, zoomLevel));