缩放以适应地图上的所有标记谷歌地图v2 android

时间:2014-11-24 17:19:31

标签: java android google-maps google-maps-api-3 google-maps-api-2

我试图实现适合所有标记点的缩放,这些标记点来自数据库,我制作了一系列标记,但是当我运行应用程序时,它只让我放大一个标记,我在这里缺少什么?

public class MapaViagem extends FragmentActivity {

    private GoogleMap googleMap;
    private String rm_IdViagem;
    private List<ClienteModel> mClienteModel = new ArrayList<ClienteModel>();
    private ViagemModel mViagemModel = new ViagemModel();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.maps);

        try {

            Bundle parametros = getIntent().getExtras();
            rm_IdViagem = parametros.getString("id_viagem");

            Repositorio ca = new Repositorio(this);
            mViagemModel = ca.getViagemPorId(Integer.valueOf(rm_IdViagem));

            Repositorio cl = new Repositorio(this);
            mClienteModel = cl.getClientesViagem(Integer.valueOf(rm_IdViagem));

            for (int i = 0; i < mClienteModel.size(); i++) {

                Repositorio mRepositorio = new Repositorio(this);
                EnderecoModel mEnderecoModel = mRepositorio.getEnderecosDoCliente(Integer.valueOf(mClienteModel.get(i).getClientes_id()));


                // Loading map
                initilizeMap();
                // Changing map type
                googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                // googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                // googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                // googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                // googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);

                // Showing / hiding your current location
                googleMap.setMyLocationEnabled(true);

                // Enable / Disable zooming controls
                googleMap.getUiSettings().setZoomControlsEnabled(false);

                // Enable / Disable my location button
                googleMap.getUiSettings().setMyLocationButtonEnabled(true);

                // Enable / Disable Compass icon
                googleMap.getUiSettings().setCompassEnabled(true);

                // Enable / Disable Rotate gesture
                googleMap.getUiSettings().setRotateGesturesEnabled(true);

                // Enable / Disable zooming functionality
                googleMap.getUiSettings().setZoomGesturesEnabled(true);

                float latitude = Float.parseFloat(mEnderecoModel.getLatitude());
                float longitude = Float.parseFloat(mEnderecoModel.getLongitude());


                // before loop:
                List<Marker> markers = new ArrayList<Marker>();

                Marker marker = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)));
                markers.add(marker);

                markers.size();


                final LatLngBounds.Builder builder = new LatLngBounds.Builder();
                for (Marker m : markers) {
                    builder.include(m.getPosition());
                }
                LatLngBounds bounds = builder.build();


                final CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 25, 25, 0);
                googleMap.animateCamera(cu);


            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Não foi possível carregar o mapa", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }


}

1 个答案:

答案 0 :(得分:0)

您只在此处添加一个标记:

            List<Marker> markers = new ArrayList<Marker>();

            Marker marker = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)));
            markers.add(marker);

并且没有人。因此,您的列表只有一个标记,地图只显示一个标记。