Problem with google maps api getMap () - android

1

I'm studying google maps api and was using as a basis for my studies video-lessons made available on youtube. However, while I was studying I came across a code depreciation problem. The video lesson you were following is using a function that no longer exists in the current version of android. I would like you to help me adapt the code I have for it to work correctly. The function in question is the "getMap ()" of the API that I tried to override for getMapAsync () (which, according to the documentation, is the equivalent function), but which also continued with error. The original code in question is this:

public class MainActivity extends FragmentActivity{

private GoogleMap map;
private SupportMapFragment mapFragment;

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

    GoogleMapOptions options = new GoogleMapOptions();
    options.zOrderOnTop(true);

    mapFragment = SupportMapFragment.newInstance(options);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.llContainer, mapFragment);
    ft.commit();
}

public void onResume(){
    super.onResume();

    new Thread(){
        public void run(){

            while(mapFragment.getMap() == null){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    configMap();
                }
            });

        }
    }.start();
}

public void configMap(){

    map = mapFragment.getMap();
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);


    LatLng latLng = new LatLng(-23, -46);
    CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(18).build();
    CameraUpdate update = CameraUpdateFactory.newCameraPosition(cameraPosition);

    map.animateCamera(update, 3000, new GoogleMap.CancelableCallback() {
        @Override
        public void onFinish() {
            Log.i("Script", "CancelableCallback.OnFinish");
        }

        @Override
        public void onCancel() {
            Log.i("Script", "CancelableCallback.OnCancel");
        }
    });


}

Can anyone help me with this problem, what changes do I need to make to code work without error?

    
asked by anonymous 18.04.2017 / 23:58

1 answer

1

Android evolves very fast and a lot of code goes by the way. This is the problem of following very old tutorials.

It may even be that the rest of the article / video is still relevant, but the code has lagged behind and I believe it can not be adapted, because the differences between getMap () and getMapAsync () are rightly reflected in this code.

The newest version, gets the map of asynchronous form , which is what this snippet does:

new Thread(){
    public void run(){

        while(mapFragment.getMap() == null){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                configMap();
            }
        });

    }
}.start();

See this documentation page, in Portuguese with code updated for get a googlemap.

public class MapPane extends Activity implements OnMapReadyCallback {

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

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

@Override
public void onMapReady(GoogleMap map) {
    LatLng sydney = new LatLng(-33.867, 151.206);

    map.setMyLocationEnabled(true);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

    map.addMarker(new MarkerOptions()
            .title("Sydney")
            .snippet("The most populous city in Australia.")
            .position(sydney));
 }
}
    
20.04.2017 / 18:07