androidgoogle-mapsgoogle-maps-markersgoogle-maps-android-api-2

How to display a Google Maps Android Marker on top of the others?


I have multiple markers in my application, sometimes some of them are at the same position. I want some of those markers to be always displayed in front of the others. How should I proceed ?

Thank you in advance.


Solution

  • I think this is the wrong approach. Even if you put one marker in front of the other there is still a possibility that the user will click the marker that is behind.

    I would instead of doing that, on adding a new Marker to the map, check if there is already a Marker at the newly added Marker location, if yes just added the new data to the current Marker and don't added another one.

    that way the same marker will represent both specified locations.

    UPDATE:

    Take a look at this code, in it I check if the currently clicked marker is my current location. And this way I know if I should fire up a directions activity or not. You can use the same technic to find out if you already have a marker at specific location.

     map.setInfoWindowAdapter(new InfoWindowAdapter() {
    
                // Use default InfoWindow frame
                @Override
                public View getInfoWindow(Marker args) {
                    return null;
                }
    
                // Defines the contents of the InfoWindow
                @Override
                public View getInfoContents(Marker args) {
    
                    // Getting view from the layout file info_window_layout
                    View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
    
                    // Getting the position from the marker
                    clickMarkerLatLng = args.getPosition();
    
                    TextView title = (TextView) v.findViewById(R.id.tvTitle);
                    title.setText(args.getTitle());
    
                    map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {          
                        public void onInfoWindowClick(Marker marker) 
                        {
                            if (SGTasksListAppObj.getInstance().currentUserLocation!=null)
                            {   
                                if (String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLatitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) &&
                                        String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLongitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
                                {
                                    Toast.makeText(getApplicationContext(), "This your current location, navigation is not needed.",  Toast.LENGTH_SHORT).show();
                                }
                                else
                                {
                                    FlurryAgent.onEvent("Start navigation window was clicked from daily map");
                                    tasksRepository = SGTasksListAppObj.getInstance().tasksRepository.getTasksRepository();
                                    for (Task tmptask : tasksRepository)
                                    {
                                        String tempTaskLat = String.valueOf(tmptask.getLatitude());
                                        String tempTaskLng = String.valueOf(tmptask.getLongtitude());
    
                                        Log.d(TAG, String.valueOf(tmptask.getLatitude())+","+String.valueOf(clickMarkerLatLng.latitude).substring(0, 8));
    
                                        if (tempTaskLat.contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) && tempTaskLng.contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
                                        {  
                                            task = tmptask;
                                            break;
                                        }
                                    }
                                    /*
                                    findDirections(SGTasksListAppObj.getInstance().currentUserLocation.getLatitude(),
                                                   SGTasksListAppObj.getInstance().currentUserLocation.getLongitude(),
                                                   clickMarkerLatLng.latitude, clickMarkerLatLng.longitude, GMapV2Direction.MODE_DRIVING );
                                    */
                                    Intent intent = new Intent(getApplicationContext() ,RoadDirectionsActivity.class);
                                    intent.putExtra(TasksListActivity.KEY_ID, task.getId());
                                    startActivity(intent);
    
                                }
                            }
                            else
                            {
                                Toast.makeText(getApplicationContext(), "Your current location could not be found,\nNavigation is not possible.",  Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
    
                    // Returning the view containing InfoWindow contents
                    return v;
    
                }
            });