Google maps' getMap() problem - android

Asked

Viewed 192 times

1

I am studying the google maps api and was using it as a basis for my video studies available on youtube. However, while I was studying I came across a code depreciation problem. The video class that was 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 that I have to make it work properly. The function in question is the "getMap()" of the API that I tried to replace 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, which changes I need to make to the code work without error?

1 answer

1


Android evolves very fast and a lot of code gets in 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 is behind and I believe that it cannot be adapted, why the differences between getMap() and getMapAsync() are precisely reflected in this code.

Newest version, get the shape map asynchronous, which is what this piece of code 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();

Look at this page of the documentation, in Portuguese with updated code to 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));
 }
}
  • Thank you for responding!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.