MSP MSP - 9 months ago 62
Android Question

How to use getMapAsync() instead of getMap()

I am aware that in Play Services,

getMap()
is depreciated, and is replaced by
getMapAsync()
.

But I am not sure how to use
getMapAsync()
. I found other questions similar to this -- however, in my code, I am not sure what to implement in my
getMapAsync()
. I think it is supposed to be a callback method, but I am not sure.

Could someone please put the correct code in, and tell me what I am doing wrong?

public class FindParty extends AppCompatActivity {

static final LatLng Durban = new LatLng(-29.858680, 31.021840);
private GoogleMap googleMap;

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


try{

if (googleMap == null) {

googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync();

}

googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setTrafficEnabled(true);
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);

final Marker marker_Durban = googleMap.addMarker(new MarkerOptions()
.position(Durban)
.snippet("Durban, KwaZulu-Natal, South Africa")
.title("Durban"));

googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

@Override
public void onInfoWindowClick(Marker marker) {
if (marker.getTitle().equals("Durban")) {
Intent intent = new Intent(FindParty.this, Party.class);
intent.putExtra("message", "Durban");
startActivity(intent);
}
}
});

} catch (Exception e) {

e.printStackTrace();
}

}
}

Answer Source

your class need to implements OnMapReadyCallback

  public class FindParty extends AppCompatActivity implements OnMapReadyCallback {

    static final LatLng Durban = new LatLng(-29.858680, 31.021840);
    private GoogleMap googleMap;
    MapFragment mapFragment;

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


        try {

            if (googleMap == null) {

                mapFragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
                mapFragment.getMapAsync(this);

            }

        } catch (Exception e) {

            e.printStackTrace();
        }

    }

    @Override
    public void onMapReady(GoogleMap map) {
        googleMap = map;
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        googleMap.setTrafficEnabled(true);
        googleMap.setIndoorEnabled(true);
        googleMap.setBuildingsEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);

        final Marker marker_Durban = googleMap.addMarker(new MarkerOptions()
                .position(Durban)
                .snippet("Durban, KwaZulu-Natal, South Africa")
                .title("Durban"));

        googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

            @Override
            public void onInfoWindowClick(Marker marker) {
                if (marker.getTitle().equals("Durban")) {
                    Intent intent = new Intent(FindParty.this, Party.class);
                    intent.putExtra("message", "Durban");
                    startActivity(intent);
                }
            }
        });

    }
}