Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Required com.google.android.gms.GoogleMap found void

I have looked online to find a solution as getMap() method is Deprecated, therefore, I would like to replace it with getMapAsync();. I have searched on google as well as androids official documentations and I am still stuck please help.

I am now getting one error message which says incompatible type Required com.google.android.gms.GoogleMap found void.

public class Maps extends AppCompatActivity implements OnMapReadyCallback {
    private GoogleMap googleMap;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.Maps);

        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                R.id.mapFragment)).getMapAsync(this);


        LatLng latlong = new LatLng(lat, lon);
        googleMap.addMarker(new MarkerOptions().position(latlong).title(name));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlong, 15));
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

    }
}

This method below gives the error and it says incompatible type Required com.google.android.gms.GoogleMap found void.

googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                R.id.mapFragment)).getMapAsync(this);
like image 706
Maddie_J Avatar asked Jan 06 '23 16:01

Maddie_J


1 Answers

getMapAsync has as return type void, and you are trying to assign it

googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                R.id.mapFragment)).getMapAsync(this);

to googleMap. Which doesn't make much sense. Call only getMapAsync like

((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                R.id.mapFragment)).getMapAsync(this);

and assign googleMap when onMapReady is invoked. E.g.

@Override
public void onMapReady(GoogleMap googleMap) {
    this.googleMap = googleMap;
}
like image 58
Blackbelt Avatar answered Jan 15 '23 23:01

Blackbelt