Clickable Google Maps marker in Fragment

2019-08-26 04:33发布

问题:

What I am trying to accomplish here is be able to show a AlertDialog when I click on a marker that is dynamically added on load (call an API, get positions and show them on map). This is done in the fragments onCreateView.

The map loads here:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    FragmentManager fm = getChildFragmentManager();
    mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
    if (mapFragment == null) {
        mapFragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.map_container, mapFragment).commit();
    }
}

Later on, I switch from a SupportMapFragment to a GoogleMap:

@Override
public void onResume() {
    super.onResume();
    if (mMap == null && mapFragment != null) {
        mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mMap = Tools.configBasicGoogleMap(googleMap);
                mMap.setMapType(sharedPref.getMapType());
            }
        });
    }
}

So, logically, my Map should be ready as mMap. Now, I want to show a AlertDialog, so I do implements GoogleMap.OnMarkerClickListener in the class definition for the fragment and implement it here:

@Override
public boolean onMarkerClick(final Marker marker) {
    Toast.makeText(getContext(), "Hello world", Toast.LENGTH_SHORT).show();
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setTitle("Confirmation");
    builder.setMessage("Pour confirmer le rapport, appuyez sur + 1.\nSi le rapport est faux, cliquez sur - 1.");
    builder.setPositiveButton("+ 1", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id){
            URL link = null;
            try {
                link = new URL(S.base_url + "report/increment/" + marker.getTag().toString());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            try {
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(
                                link.openStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    AlertDialog ab = builder.create();
    ab.show();
    return false;
}

But not even the Toast doesn't show up... When I click on a marker I get the Directions and Open in Google Maps buttons, and the map centers itself on the marker, but my method doesn't start.

To conclude, logically when I click one of these markers that I load, the method onMarkerClick should trigger itself, but this never happens! Any idea why? Thanks.

回答1:

I don't know why but Google Maps API can't used as implement to fragments. Following code may work it for you. You just need to assign markerClickListener for local, not implementing MarkerClickListener.

   //Detect location and set on map
    mMapView.getMapAsync(new OnMapReadyCallback() {
        @SuppressLint("MissingPermission")
        @Override
        public void onMapReady(GoogleMap mMap) {
            googleMap = mMap;
            // Bla
            // Bla
            // Bla your codes about map
            googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
                    // Triggered when user click any marker on the map
                    return false;
                }
            });
        }
    });