使用bounds动画相机会返回nullPointerException

时间:2014-05-28 00:29:32

标签: android google-maps bounds

我正在开发一款需要同时查看各种地图点的应用。我试图用边界和Builder动画相机,但它每次都返回null,我检查坐标和.build(),所有这些都有数据。这是代码。

LatLngBounds.Builder builderMap = new LatLngBounds.Builder();           
            LocationManager locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
            Criteria criteria = new Criteria();
            String bestProvider = locationManager.getBestProvider(criteria, false);
            android.location.Location location = locationManager.getLastKnownLocation(bestProvider);
            if(location!=null)
            {
                Log.i("MapACtivity","builder includes myLocation");
                builderMap.include(new LatLng(location.getLatitude(),location.getLongitude()));             
                coordinate_bounds= coordinate_bounds +1;
            }
            for(CircleContact myContact : newCircle.getMembers())
            {
                if(myContact.getLatitude()!=0 && myContact.getLongitude()!=0)
                {
                    Log.i("MapACtivity","builder includes locations " + myContact.getDevice());
                    Log.i("MapACtivity","Lat " + myContact.getLatitude()+ " longitude" +myContact.getLongitude());
                    builderMap.include(new LatLng(myContact.getLatitude(),myContact.getLongitude()));
                    coordinate_bounds= coordinate_bounds +1;
                }
            }
            if(coordinate_bounds>1)
            {
                Log.i("CircleFragment","builderMap.build()" + builderMap.build().getCenter());
                Log.i("CircleFragment","builderMap.build()" + builderMap.build().northeast);
                Log.i("CircleFragment","builderMap.build()" + builderMap.build().southwest);                
                mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builderMap.build(), 50));                                                           
            }

我得到的日志是:

builderMap.build()lat/lng: (-11.075408249999999,-75.85188305)
builderMap.build()lat/lng: (-10.078819,-74.748112)
builderMap.build()lat/lng: (-12.0719975,-76.9556541)

但它在这一行中返回null:

mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builderMap.build(), 50));                                                           

并且它不是地图,因为在我将相机更新到我的位置之前,并且没有崩溃,这是在尝试获取地图内的所有位置之前的代码并且它正常工作:

MapView mMapView;
mMapView = (MapView)getActivity().findViewById(R.id.map_locations);                                         
mMap = mMapView.getMap();           
mMap.addMarker(myMarker);                           
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(Location, 15);                            
mMap.animateCamera(yourLocation);

1 个答案:

答案 0 :(得分:1)

这来自官方谷歌地图android页面:

  

注意:如果要在地图经过布局后用于移动相机,则仅使用更简单的方法newLatLngBounds(boundary,padding)生成CameraUpdate。在布局期间,API会计算正确投影边界框所需的地图显示边界。相比之下,您可以随时使用更复杂的方法newLatLngBounds(边界,宽度,高度,填充)返回的CameraUpdate,甚至在地图经历布局之前,因为API会根据您传递的参数计算显示边界。

这就是他们设定界限的方式:

private GoogleMap mMap;
private LatLngBounds AUSTRALIA = new LatLngBounds(
new LatLng(-44, 113), new LatLng(-10, 154));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(AUSTRALIA.getCenter(), 10));