SmiglowiecX SmiglowiecX - 6 months ago 66
Android Question

Multiple Google Maps Lite in FirebaseRecycler

I tried to make it running, by the only thing I got is a grey grid with Google logo on in the left bottom corner in the place of fragment. Maps are intented to be displayed in CardView loaded by FirebaseRecyclerAdapter. There is no errors thrown and the API key is correct. When I scroll the items fast up and down several times, the maps appear to load slowly on each scroll. When left without scrolling, nothing loads.

The card's XML is:

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="1dp"
card_view:cardCornerRadius="0dp"
android:layout_marginLeft="-3dp"
android:layout_marginRight="-3dp"
android:id="@+id/cv">
<com.google.android.gms.maps.MapView
class="com.google.android.gms.maps.MapFragment"
android:id="@+id/mapLite"
android:layout_width="match_parent"
android:layout_height="150dp"
map:mapType="normal"
map:liteMode="true"/>
</android.support.v7.widget.CardView>
</LinearLayout>


The code for Firebase adapter is as follows:

adapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(Post.class, R.layout.card_view_item, PostViewHolder.class, ref.child("message").child(currentGroupKey)) {

@Override
protected void populateViewHolder(final PostViewHolder postViewHolder, final Post post, int position) {
super.populateViewHolder(postViewHolder, post, position);
}
};


And the code for PostViewHolder:

public class PostViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {
public MapView mapView;


public PostViewHolder(View itemView) {
super(itemView);

mapView = (MapView) itemView.findViewById(R.id.mapLite);

mapView.onCreate(null);
mapView.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
CameraUpdate updateCamera = CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng(40.712784, -74.005941), 10, 0f, 0));
googleMap.moveCamera(updateCamera);
}
}


Does anybody know what may be the issue?

Answer

I've found the answer! The problem was a wrong namespace in XML. I've found the resolution by studying this example: https://github.com/saxman/android-map_list. Bellow I post the right version of files showed above in case somebody have the similar problem:

Card's XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:cardUseCompatPadding="true"
    card_view:cardElevation="1dp"
    card_view:cardCornerRadius="0dp"
    android:layout_marginLeft="-3dp"
    android:layout_marginRight="-3dp"
    android:id="@+id/cv">
            <com.google.android.gms.maps.MapView
                android:name="com.google.android.gms.maps.MapFragment"
                android:id="@+id/mapLite"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                map:liteMode="true"/>
    </LinearLayout>
</android.support.v7.widget.CardView>

The code for Firebase adapter:

adapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(Post.class, R.layout.card_view_item, PostViewHolder.class, ref.child("message").child(currentGroupKey)) {
    @Override
    public void onBindViewHolder(PostViewHolder viewHolder, int position) {
        super.onBindViewHolder(viewHolder, position);
        viewHolder.setMapLocation(52.235474, 21.004057);
    }

    @Override
    protected void populateViewHolder(final PostViewHolder postViewHolder, final Post post, int position) {
        super.populateViewHolder(postViewHolder, post, position);
    }
};

And the PostViewHolder:

    public class PostViewHolder extends RecyclerView.ViewHolder {
    public MapView mapView;

    protected GoogleMap mGoogleMap;
    protected LatLng mMapLocation;

    public PostViewHolder(final View itemView) {
        super(itemView);
        mapView = (MapView) itemView.findViewById(R.id.mapLite);

        mapView.onCreate(null);
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {

                mGoogleMap = googleMap;
                MapsInitializer.initialize(itemView.getContext());
                googleMap.getUiSettings().setMapToolbarEnabled(true);
                if (mMapLocation != null) {
                    updateMapContents();
                }
            }
        });
    }

    public void setMapLocation(double lat, double lon) {
        mMapLocation = new LatLng(lat, lon);

        if (mGoogleMap != null) {
            updateMapContents();
        }
    }

    protected void updateMapContents() {
        mGoogleMap.clear();
        // Update the mapView feature data and camera position.
        mGoogleMap.addMarker(new MarkerOptions().position(mMapLocation));
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(mMapLocation, 10f);
        mGoogleMap.moveCamera(cameraUpdate);
    }
}
Comments