Android用轴承映射V2 newLatLngBounds

时间:2013-01-31 17:10:10

标签: android android-maps-v2

我正在使用具有此布局的新Android地图V2:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:map="http://schemas.android.com/apk/res-auto"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.SupportMapFragment"
  map:cameraBearing="270"/>

我正在尝试使用newLatLngBounds(LatLngBounds bounds,int padding)方法来缩放和查看地图中的所有标记,但相机方位设置为0.

Google开发者文档中的描述说:

  

public static CameraUpdate newLatLngBounds(LatLngBounds bounds,int   填充)(...)。返回的CameraUpdate的轴承为0和a   倾斜0.(...)“。

如何更改轴承值?

我试图在调用newLatLngBounds之后以编程方式设置一个新轴承,如下所示:

mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
.target(mMap.getCameraPosition().target)
.zoom(mMap.getCameraPosition().zoom)
.bearing(270)
.build()));

但是当我这样做时,一些标记将不会出现。

3 个答案:

答案 0 :(得分:12)

我有另一种选择,我有同样的问题。我将展示如何将边界转换为缩放,然后我们只需使用CameraPosition.builder()来创建正确的位置。

private static final double LN2 = 0.6931471805599453;
    private static final int WORLD_PX_HEIGHT = 256;
    private static final int WORLD_PX_WIDTH = 256;
    private static final int ZOOM_MAX = 21;

    public int getBoundsZoomLevel(LatLngBounds bounds, int mapWidthPx, int mapHeightPx){

        LatLng ne = bounds.northeast;
        LatLng sw = bounds.southwest;

        double latFraction = (latRad(ne.latitude) - latRad(sw.latitude)) / Math.PI;

        double lngDiff = ne.longitude - sw.longitude;
        double lngFraction = ((lngDiff < 0) ? (lngDiff + 360) : lngDiff) / 360;

        double latZoom = zoom(mapHeightPx, WORLD_PX_HEIGHT, latFraction);
        double lngZoom = zoom(mapWidthPx, WORLD_PX_WIDTH, lngFraction);

        int result = Math.min((int)latZoom, (int)lngZoom);
        return Math.min(result, ZOOM_MAX);
    }

    private double latRad(double lat) {
        double sin = Math.sin(lat * Math.PI / 180);
        double radX2 = Math.log((1 + sin) / (1 - sin)) / 2;
        return Math.max(Math.min(radX2, Math.PI), -Math.PI) / 2;
    }
    private double zoom(int mapPx, int worldPx, double fraction) {
        return Math.floor(Math.log(mapPx / worldPx / fraction) / LN2);
    }

然后您可以使用此代码并添加带有边界的方位/和/或倾斜:

            LatLngBounds bounds = LatLngBounds.builder()
                    .include(swCorner)
                    .include(neCorner).build();
            CameraPosition cp = new CameraPosition.Builder()
                    .bearing(randomBearing())
                    .tilt(randomTilt())
                    .target(latLng)
                    .zoom(getBoundsZoomLevel(bounds, findViewById(R.id.map).getMeasuredWidth(), findViewById(R.id.map).getMeasuredHeight()))
                    .build();
            CameraUpdate cu = CameraUpdateFactory.newCameraPosition(cp);
            mMap.animateCamera(cu);

这将有效(您必须将mMap更改为实际的地图变量,并将ID更改为地图ID)。

祝你好运!我从帖子How do I determine the zoom level of a LatLngBounds before using map.fitBounds?获得了boundZoomLevel 如果你有时间,那就去那样的答案吧!

答案 1 :(得分:4)

是的,你可以! 只需使用: https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap#animateCamera(com.google.android.gms.maps.CameraUpdate,int,com.google.android.gms.maps.GoogleMap.CancelableCallback)

并实现CancelableCallback并放入onFinish()方法: https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.CancelableCallback#onFinish()

更改轴承的代码。 当然你不会得到一个动画而是一个两个链,但它会起作用!

mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100)/*,DURATION_IN_MS_IF_NEEDED*/,new GoogleMap.CancelableCallback(){
    @Override
    public void onCancel() {
        //DO SOMETHING HERE IF YOU WANT TO REACT TO A USER TOUCH WHILE ANIMATING
    }
    @Override
    public void onFinish() {
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
/*.target(mMap.getCameraPosition().target)
.zoom(mMap.getCameraPosition().zoom)*/
.bearing(270)
.build()));
    }
});

我认为你不需要目标和缩放,我将它们注释掉,因为它应该由上一个动画管理

修改 更好地了解API,我有一种方法可行(我现在无法测试它,请看一下: 拥有LatLngBounds你可以得到最适合该区域的缩放级别(它取决于设备尺寸,因此你必须在运行时获得视图尺寸),然后,当你有缩放(你也有中心) LatLngBounds),您可以使用一个独特的动画进行相机更新: https://developers.google.com/android/reference/com/google/android/gms/maps/model/CameraPosition,构造函数允许使用这组值,或者您可以使用之前看到的构建器。

您可以在线找到各种方法,例如(只需在线快速搜索获得javascript版本https://stackoverflow.com/a/13274361/4120431

希望这可以帮助您解决问题!

答案 2 :(得分:-3)

Google的CameraUpdateFactory上的错误问题 我用时间,微小的时间解决了它!

public void zoomToBounds() {
    LatLngBounds latLngBounds = getBoundsOfInput(inputPoints);
    final int PADDING = 20;

    final CameraPosition now = map.getCameraPosition();
    final float tilt = map.getCameraPosition().tilt;
    final float bearing = map.getCameraPosition().bearing;
    final LatLng[] target = new LatLng[1];
    final float[] zoom = new float[1];

    map.animateCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, PADDING), 1, null);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            target[0] = map.getCameraPosition().target;
            zoom[0] = map.getCameraPosition().zoom;
        }
    }, 10);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            map.animateCamera(CameraUpdateFactory.newCameraPosition(now), 1, null);
        }
    }, 30);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            map.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(target[0], zoom[0], tilt, bearing)));
        }
    }, 60);
}

看起来很脏,但有效

3 Handler完成工作,每个postdelay必须大于之前的