0

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();
        }

    }
}
MSP
  • 89
  • 1
  • 2
  • 10

4 Answers4

2

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);
                }
            }
        });

    }
}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
0

You are right, getMapAsync() is based on callback, please see belowe example and move your map options settings to callback method:

Example

Community
  • 1
  • 1
anddev
  • 206
  • 2
  • 11
0

getMapAsync() { it.isTrafficEnabled = false }

Ilja Popov
  • 1,091
  • 8
  • 8
0

The better way I found to understand the use of getMapAsync was to create a new project in Android Studio and use the template of google maps activity instead of an empty activity. Hope help others with the same difficulty.

Chip
  • 1
  • 2