在SupportMapFragment

时间:2017-09-12 06:50:26

标签: android android-layout android-fragments google-maps-android-api-2 supportmapfragment

我想知道在点击地图中的标记时如何隐藏/显示FloatingActionButton。

以下是地图代码:

public class MapsActivity extends SupportMapFragment implements 
OnMapReadyCallback, GoogleMap.OnMapClickListener, 
GoogleMap.OnMarkerClickListener, GoogleMap.OnInfoWindowClickListener {

private static final String TAG = "Maps";
private GoogleMap mMap;
private LocationManager locationManager;
private ArrayList<LatLng> latlngs = new ArrayList<>();
private MarkerOptions markerOptions = new MarkerOptions();
private Marker markerN;
private Marker markerO;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {

    try {
        Criteria criteria = new Criteria();
        locationManager = (LocationManager) 
getActivity().getSystemService(Context.LOCATION_SERVICE);
        mMap = googleMap;
        mMap.setOnMapClickListener(this);
        mMap.setOnMarkerClickListener(this);
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.getUiSettings().setMapToolbarEnabled(false);
        mMap.setMyLocationEnabled(true);
        Location location = 
locationManager.getLastKnownLocation(locationManager
                .getBestProvider(criteria, false));
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        LatLng sydney = new LatLng(latitude, longitude);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 15.0f));
        mMap.setOnInfoWindowClickListener(this);
    } catch (SecurityException ex) {
        Log.e(TAG, "Error", ex);
        requestPermissions(new String[]
{Manifest.permission.ACCESS_FINE_LOCATION},
                1);
    } catch (Exception e) {
        System.out.println(e);
    }

    try {
        // Customise the styling of the base map using a JSON object defined
        // in a raw resource file.
        boolean success = mMap.setMapStyle(
                MapStyleOptions.loadRawResourceStyle(
                        getContext(), R.raw.style_json));

        if (!success) {
            Log.e("MapsActivityRaw", "Style parsing failed.");
        }
    } catch (Resources.NotFoundException e) {
        Log.e("MapsActivityRaw", "Can't find style.", e);
    }

    latlngs.add(new LatLng(-22.978608, -49.869901));
    for (LatLng point : latlngs) {
        markerOptions.position(point);
        markerOptions.title("Local");
        markerOptions.snippet("Info");
        markerO = mMap.addMarker(markerOptions);
    }
}

@Override
public void onMapClick(LatLng latLng) {
    mMap.clear();
    markerOptions.position(latLng);

    markerOptions.title("Deseja cadastrar este local?");
    markerOptions.snippet("Pressione aqui");
    markerN = mMap.addMarker(markerOptions);
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));
    Toast.makeText(getContext(), "Coord: " + latLng.toString(), 
Toast.LENGTH_LONG).show();
}

@Override
public void onRequestPermissionsResult(int requestCode, String 
permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
{

            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }
        // other 'case' lines to check for other
        // permissions this app might request
    }
}

@Override
public void onInfoWindowClick(Marker marker) {
    if (marker.equals(markerN)) {
        Toast.makeText(getContext(), "Clique 2", Toast.LENGTH_LONG).show();
    } else {

    }
}

@Override
public boolean onMarkerClick(Marker marker) {
    if (marker.equals(markerN)) {
        Toast.makeText(getContext(), "Clique 1", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getContext(), "Abrir Informações", 
Toast.LENGTH_LONG).show();
    }
    return false;
}
}

这就是我想要互动的XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alan.unigeo.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

我尝试过寻找类似情况的例子但却失败了。 地图位于布局的片段内,而FloatingActionButton位于前一个“上方”的另一个布局中。我不知道是否需要显示任何其他代码来帮助你们,如果需要,只需要它。

1 个答案:

答案 0 :(得分:0)

我的程序中有这个功能。

如果您不想显示它,请使用以下代码:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ((FloatingActionButton) getView().getRootView().findViewById(R.id.fab)).hide();
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    ((FloatingActionButton) getView().getRootView().findViewById(R.id.fab)).show();
}

如果您想要显示它,只需切换 .show() .hide()