sean
sean

Reputation: 3634

Google Maps Android API - Two Maps with getMapAsync()

I have two maps in one view fragment (see screenshot below), and although the maps run correctly and I can access one map via getMapAsync(), I am unable to understand how to run another getMapAsync() and distinguish it from the first.

Two maps together working well:

Two Google Maps (Android) on One View Fragment

A fairly simple View Layout (called "fragment_map.xml"):

<com.google.android.gms.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <android.support.v7.widget.CardView
        android:id="@+id/map_card"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp">
        <com.google.android.gms.maps.MapView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/miniMap"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="0dp" />
    </android.support.v7.widget.CardView>

In the Activity (or Fragment in my case):

    private MapView mv, miniMap;
    private GoogleMap gm;
    private View myLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        myLayout = inflater.inflate(R.layout.fragment_map, container, false);

        mv = (MapView) myLayout.findViewById(R.id.mapview);
        mv.onCreate(savedInstanceState);

        miniMap = (MapView) myLayout.findViewById(R.id.miniMap);
        miniMap.onCreate(savedInstanceState);

        mv.getMapAsync(this);
        miniMap.getMapAsync(this);

        return myLayout;
    }

    @Override
    public void onMapReady(GoogleMap map) {
        gm = map;
        gm.setMyLocationEnabled(true);

        gm.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition position) {
                gm.animateCamera(CameraUpdateFactory.newLatLng(position.target));
            }
        });
    }

The maps inflate correctly and everything works smoothly, but as you can guess, whatever I do to "GoogleMap gm" happens to both maps due to

mv.getMapAsync(this);
miniMap.getMapAsync(this);

both pointing to a single

@Override
public void onMapReady(GoogleMap map) {

Any ideas as to how I can attach a separate GoogleMap to each MapView?


EDIT:

Thanks to Zeeshan Shabbir I used a boolean to first target the main map, then initialize the mini-map:

private GoogleMap gm;
private View myLayout;
private boolean miniMapReady = false;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    myLayout = inflater.inflate(R.layout.fragment_map, container, false);

    mv = (MapView) myLayout.findViewById(R.id.mapview);
    mv.onCreate(savedInstanceState);

    miniMap = (MapView) myLayout.findViewById(R.id.miniMap);
    miniMap.onCreate(savedInstanceState);

    mv.getMapAsync(this);

    return myLayout;
}

@Override
public void onMapReady(GoogleMap map) {

    if (!miniMapReady) {
        gm = map;
        gm.setMyLocationEnabled(true);
        miniMap.getMapAsync(this);
        miniMapReady = true;
    } else {
        miniGm = map;
    }
    gm.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition position) {
            miniGm.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(position.target, position.zoom - 5.0f, position.tilt, position.bearing)));
        }
    });
}

The ChangeCamera Listener here allows the mini-map to follow the user's interaction on the main map while remaining at a lower zoom level.

Upvotes: 1

Views: 833

Answers (1)

Zeeshan Shabbir
Zeeshan Shabbir

Reputation: 7114

Try to use boolean when minimap is ready set it to false. And in onMapReady check that boolean if it is true then do what you want for mv otherwise if boolean is false write logic for minimap

Upvotes: 3

Related Questions