Android Google Maps v2: on click listener not responding

18,088

Solution 1

If you have extended SupportMapFragment you can simply do this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ActionBarActivity activity = (ActionBarActivity) getActivity();
    activity.getSupportActionBar().setTitle(R.string.select_location);  
    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);

  map = getMap();
  map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
  map.setOnMapClickListener(new OnMapClickListener() {

    @Override
    public void onMapClick(LatLng point) {
        Log.d("Map","Map clicked");
        marker.remove();
        drawMarker(point);
    }
});

Note that getMap() is called in onActivityCreated and inflater.inflate(R.layout.map, container, false) is not necessary if you don´t use a custom layout.

You don´t even need a map.xml layout!

You are extending SupportMapFragment but you are inflating another MapView (not the one tied to SupportMapFragment by default), that´s why you are not viewing the changes in your map. Because you were acting on the default View got from getMap() but you are viewing another. See the docs about getMap():

public final GoogleMap getMap ()

Gets the underlying GoogleMap that is tied to the view wrapped by this fragment.

Hope it helps ;)

Solution 2

Well you are placing a listener for your map, but you need to make a listener for your markers.

map.setOnMarkerClickListener(this);

...

@Override
public boolean onMarkerClick(Marker arg0) {     
    Log.i(TAG,"marker arg0 = "+arg0);               
    return false;
}

or for the InfoWindows on top of the markers:

map.setOnInfoWindowClickListener(this);

Also, your initialitizing of map making it show satelite is almost correct:

map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

change it to:

map.setMapType(GoogleMap.MAP_TYPE_HYBRID);

Solution 3

Instead of doing map = getMap(), a few lines later I had this code:

        SupportMapFragment fm = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
        map = fm.getMap();

So I've just put the code above that line and it's done.

Share:
18,088
Jon Zangitu
Author by

Jon Zangitu

Basketball passionate and professional developer.

Updated on July 29, 2022

Comments

  • Jon Zangitu
    Jon Zangitu almost 2 years

    I'm trying to put a marker when the user clicks on map. I'm using a SupportMapFragment inside an ActionBarActivity. But the map doesn't respond, furthermore, a map.setMapType() operation isn't working.

    Here's my code:

    private GoogleMap map;
    private Marker marker;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        ActionBarActivity activity = (ActionBarActivity) getActivity();
        activity.getSupportActionBar().setTitle(R.string.select_location);
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.map, container, false);
    }
    
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d("Map","On view created");
        map = getMap();
                map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        map.setOnMapClickListener(new OnMapClickListener() {
    
            @Override
            public void onMapClick(LatLng point) {
                Log.d("Map","Map clicked");
                marker.remove();
                drawMarker(point);
            }
        });
    
             ...
             Location location = locationManager.getLastKnownLocation(provider);
    
            if(location!=null){
               //PLACE THE INITIAL MARKER              
               drawMarker(new LatLng(location.getLatitude(),location.getLongitude()));
            }
    }
    

    Logcat is showing the "on view created" message and the map shows the current location with a marker, so the last part of the code is being executed. But the onMapClickListener is overrided or something because it doesn't work, and the map isn't a satellite.

    Can anybody help me?

  • Josu Garcia de Albizu
    Josu Garcia de Albizu over 10 years
    Hi Jon! I think you can do in an easier way, because you are already in the SupportMapFragment, it´s not necessary to get the Fragment (it´s useful when you call from the FragmentActivity). I added an answer.
  • Jon Zangitu
    Jon Zangitu over 10 years
    Thanks josuadas! The point is that I need to inflate a XML because it's not a view with just a map. I think that it's still neccesary to extend SupportMapFragment, isn't it?
  • Josu Garcia de Albizu
    Josu Garcia de Albizu over 10 years
    Ez horregatik! In my opinion you shouldn´t extend SupportMapFragment in that case but just Fragment (from support library). See this link: stackoverflow.com/questions/13804511/…
  • Josu Garcia de Albizu
    Josu Garcia de Albizu over 10 years
    Another possibility is to add the SupportMapFragment to a FragmentActivity just taking up the space for the map. F.i. using a FrameLayout as container in your FragmentActivity layout: developer.android.com/training/basics/fragments/…