CameraUpdateFactory.newLatLngBounds不workinf所有的时间 [英] CameraUpdateFactory.newLatLngBounds is not workinf all the time

查看:551
本文介绍了CameraUpdateFactory.newLatLngBounds不workinf所有的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些problema在Android谷歌地图移动相机。

I'm having some problema to move camera on android google maps.

我有一个功能:

protected void centralizeMapToBounds(final LatLng southwest, final LatLng northeast, final LatLng taxi) {
    final int padding = 30;

    Location loc1 = new Location("");
    loc1.setLatitude(southwest.latitude);
    loc1.setLongitude(southwest.longitude);

    Location loc2 = new Location("");
    loc2.setLatitude(northeast.latitude);
    loc2.setLongitude(northeast.longitude);

    Location loc3 = new Location("");
    loc3.setLatitude(taxi.latitude);
    loc3.setLongitude(taxi.longitude);

    float d1 = loc1.distanceTo(loc2);
    float d2 = loc1.distanceTo(loc3);
    float d3 = loc2.distanceTo(loc3);

    LatLngBounds bounds = null;
    try {


        if(d1 > d2){
            if(d1 > d3){
                ICabDebug.i(TAG, "Maior distancia d1: " + d1);
                if(southwest.latitude > northeast.latitude)
                    bounds = new LatLngBounds(southwest,northeast);
                else
                    bounds = new LatLngBounds(northeast, southwest);
            }else{
                ICabDebug.i(TAG, "Maior distancia d3: " + d3);
                if(northeast.latitude > taxi.latitude)
                    bounds = new LatLngBounds(northeast, taxi);
                else
                    bounds = new LatLngBounds(taxi,northeast);
            }
        }else{
            ICabDebug.i(TAG, "Maior distancia d2: " + d2);
            if(southwest.latitude > taxi.latitude)
                bounds = new LatLngBounds(southwest, taxi);
            else
                bounds = new LatLngBounds(taxi, southwest);
        }


    }catch (Exception e) {
        ICabDebug.i(TAG, e.getMessage());
        ICabDebug.i(TAG, "bounds = null");
        bounds = null;
    }

    final LatLngBounds bounds2 = bounds;

    // Pan to see all markers in view.
    try {
        ICabDebug.i(TAG, "Entro aki no primeiro bounds");
        googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
    }
    catch (Exception e) {

        if (mapView.getViewTreeObserver().isAlive()) {
            mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

                @SuppressLint("NewApi")
                @Override
                public void onGlobalLayout() {

                    mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                    try {
                        ICabDebug.i(TAG, "northeast ["+ bounds2.northeast.latitude + " / " + bounds2.northeast.longitude + "]");
                        ICabDebug.i(TAG, "southwest ["+ bounds2.southwest.latitude + " / " + bounds2.southwest.longitude + "]");
                        googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds2, padding));
                    }
                    catch (Exception ee) {
                        ICabDebug.i(TAG, "----------------------------");
                        ICabDebug.i(TAG, ee.getMessage());
                        ICabDebug.i(TAG, "----------------------------");
                        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(taxi, 15));
                    }
                }
            });
        }
    }
}

当我使用它的坐标完美的作品:
(-23.5804166,-46.681909)
(-23.4850942,-46.5802326)
(-23.5802881,-46.6819049)

When I use it with the coordinate works perfect: (-23.5804166,-46.681909) (-23.4850942,-46.5802326) (-23.5802881,-46.6819049)

但是当我使用下面的坐标,它移动相机澳大利亚:
(-23.5792416,-46.63472729999999)
(-23.5436503,-47.4519223)
(-23.580264,-46.6818923)

but when i use with the coordinate below, it moves the camera to Australia: (-23.5792416,-46.63472729999999) (-23.5436503,-47.4519223) (-23.580264,-46.6818923)

有人能帮助我吗?谢谢

推荐答案

不知道你想达到与你的问题code什么。这是一个有点杂乱。

Not sure what you want to achieve with the code in your question. It's a bit messy.

如果的要显示含3 LatLngs区,您使用的是更好的。

If you want to show area containing 3 LatLngs, you are better of using

LatLngBounds bounds = LatLngBounds.builder().include(latLng1).include(northeast).include(taxi).build();
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));

这将几行替换所有的IFS和尝试,渔获物。

This would replace all your ifs and try-catches with a few lines.

顺便说一句。你不应该在你的code做的尝试,渔获物。一个编程错误是不是你捕获并尝试不同的东西,但改变code来代替。

Btw. You shouldn't be doing try-catches in your code. A programming error is not something you catch and try something different, but change the code instead.

这篇关于CameraUpdateFactory.newLatLngBounds不workinf所有的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆