Android setOnMarkerClickListener为每个标记设置标题

时间:2014-11-27 16:44:16

标签: android google-maps google-maps-api-3 google-maps-markers markerclusterer

我在我的应用中实现了一个群集标记,我有一个来自我的数据库的LatLng列表,现在我想在用户点击标记时显示客户端的名称作为标题,但是当我点击标记它没有显示我,我怎么能实现,这是我的代码到目前为止:

    public class MapaViagem extends FragmentActivity {

    private GoogleMap googleMap;
    private String rm_IdViagem;
    private List<ClienteModel> mClienteModel = new ArrayList<ClienteModel>();
    private List<EnderecoModel> mEnderecoModel = new ArrayList<EnderecoModel>();
    private ViagemModel mViagemModel = new ViagemModel();
    private ClusterManager<MyItem> mClusterManager;


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


        setContentView(R.layout.maps);
         ArrayList<LatLng> coordList = new ArrayList<LatLng>();

        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));

            int i;

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


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


                System.out.println("NOMES " + mClienteModel.get(i).getNome());


                for (int j = 0; j < mEnderecoModel.size(); j++) {
                    // 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(true);

                    // 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);


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


                    coordList.add(new LatLng(latitude, longitude));


                    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 10));



                    // Initialize the manager with the context and the map.
                    // (Activity extends context, so we can pass 'this' in the constructor.)
                    mClusterManager = new ClusterManager<MyItem>(this, googleMap);

                    // Point the map's listeners at the listeners implemented by the cluster
                    // manager.
                    googleMap.setOnCameraChangeListener(mClusterManager);
                    googleMap.setOnMarkerClickListener(mClusterManager);








                    addItems(coordList);


                    googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

                        @Override
                        public boolean onMarkerClick(final Marker marker) {
                            LatLng pos = marker.getPosition();
                            int arryListPosition = getArrayListPosition(pos);


                            return true;
                        }
                    });



                }


            }


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

    private int getArrayListPosition(LatLng pos) {

        for (int i = 0; i < mEnderecoModel.size(); i++) {
            if (pos.latitude == Double.parseDouble(mEnderecoModel.get(i).getLatitude().split(",")[0])) {
                if (pos.longitude == Double.parseDouble(mEnderecoModel.get(i).getLongitude().split(",")[1]))
                    return i;
            }
        }
        return 0;
    }

    private void addItems(List<LatLng> markers) {

        for (int i = 0; i < markers.size(); i++) {
            MyItem offsetItem = new MyItem(markers.get(i));
            mClusterManager.addItem(offsetItem);
        }
    }



    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 :(得分:3)

不确定您是如何初始化Markers的,因为您没有显示代码的一部分但是在其上添加标题是这样做的:

//You need a reference to a GoogleMap object
GoogleMap map = ... // get a map.
Marker marker = map.addMarker(new MarkerOptions()
 .position(new LatLng(37.7750, 122.4183))     //Or whatever coordinate
 .title("Your title")
 .snippet("Extra info"));

就是这样,当你点击它时,你会看到那个标记的信息。

您可以找到更多信息here

修改

对于ClusterManager,我认为您可以找到this answer有用的