Search code examples
androidgoogle-mapsgoogle-maps-api-2android-maps-v2

Hide RelativeLayout when I touch inside a map


In my android app, I have a google maps v2 inside a fragment with marker of places. When I touch in a marker, it displays a RelativeLayout with the name of the marker. However, I would like that when I touch anywhere in the map, this RelativeLayout is hidden.

My code is this:

  • fragment_mapa.xml

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:gravity="center" />
    
    
    <RelativeLayout
        android:id="@+id/sliding_up"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#fff"
        android:orientation="vertical" 
        android:layout_alignParentBottom="true"
        android:clickable="true"
        android:focusable="false"
        android:animateLayoutChanges="true"
        android:visibility="invisible" >
    
      <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="14sp"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"/>
    </RelativeLayout>
    

  • Code where it creates the markers and onClick method to display the RelativeLayout

    public void addItemsToMap() {
        appState.mapa.clear();
        if (appState.lista.isEmpty()) {
            appState.readPlaces(5000, 0, appState.idCategoria);
        }
    
        appState.mapa.setOnMarkerClickListener(this);
        appState.mapa.setOnInfoWindowClickListener(getInfoWindowClickListener());
    
    
        LatLng miPosicion = new LatLng(obtLatitud, obtLongitud);
        appState.mapa.addMarker(new MarkerOptions()
            .position(miPosicion)
            .title("Mi posición")
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon)));
    
        for (int i = 0; i < appState.lista.size(); i++) {
            LatLng posItem = new LatLng(appState.lista.get(i).latitud,appState.lista.get(i).longitud);
            appState.mapa.addMarker(new MarkerOptions()
                .position(posItem)
                .title(appState.lista.get(i).nombre)
                .snippet(appState.lista.get(i).descripcion)
                /*.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))*/);
            Log.v("MAPA", "Marker " + i + ": " + appState.lista.get(i).nombre);
        }
    }
    
    
    @Override
    public boolean onMarkerClick(final Marker marker) {
        if(marker != null) {
            //marker.showInfoWindow();
            RelativeLayout slideLayout;
            slideLayout = (RelativeLayout) findViewById(R.id.sliding_up);
            slideLayout.setVisibility(View.VISIBLE);
    
            Animation slide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
            slideLayout.startAnimation(slide);
    
            TextView t;
            t = (TextView) findViewById(R.id.name);
            t.setText(marker.getTitle());
            return true;
    
        } else {
            RelativeLayout slideLayout;
            slideLayout = (RelativeLayout) findViewById(R.id.sliding_up);
            slideLayout.setVisibility(View.INVISIBLE);
            return false;
        }
        }
    

Solution

  • // try this :

    map.setOnMapClickListener(new OnMapClickListener() {
            @Override
            public void onMapClick(LatLng arg0) {
    
            }
        });