0

I am currently working on an Android studio app where I need a map. In this map I have a button called, add marker. What I want to do with this button is to display a marker in my current location whenever I press it.

To do that I have the next method:

private void drawMarker(Location location) {
        if (mMap != null) {
            mMap.clear();
            LatLng gps = new LatLng(location.getLatitude(), location.getLongitude());
            mMap.addMarker(new MarkerOptions().position(gps));
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(gps, 16));
        }

    }

The problem with this method is that its parameter is Location location so whenever I initialize the app and press on it, my app will stop and an error will show. I think this is because my parameter should be View view.

This is the error: java.lang.IllegalStateException: Could not find a method drawMarker(View) in the activity class com.naluapp.naluapp.MapsActivity for onClick handler on view class android.widget.Button with id 'addMarker

I tried to resolve this by putting a variable Location location in my map activity but that does no resolve my problem. I am no good at this kind of language so I need of your help. Do you know how to resolve this?

Mauricio Estevez
  • 419
  • 1
  • 6
  • 21

1 Answers1

0

The android:onClick method you reference in an XML layout must have a View parameter, and the method must be public.

Just define a Location member variable in the Activity as show here, and then reference it inside the drawMarker() method:

public void drawMarker(View v) {
    if (mMap != null && mLastLocation != null) {
        mMap.clear();
        LatLng gps = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
        mMap.addMarker(new MarkerOptions().position(gps));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(gps, 16));
    }

}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137