我试图让它运行,我唯一得到的是一个带有Google徽标的灰色网格,位于片段左下角。地图有意显示在FirebaseRecyclerAdapter加载的CardView中。没有抛出错误,API密钥正确。当我快速上下快速滚动项目时,地图似乎在每个滚动上缓慢加载。当没有滚动离开时,没有任何东西加载。
卡的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/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>
Firebase适配器的代码如下:
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);
}
};
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);
}
}
有人知道可能是什么问题吗?
答案 0 :(得分:2)
我找到了答案!问题是XML中的命名空间错误。我通过研究这个例子找到了解决方案:https://github.com/saxman/android-map_list。 Bellow我发布上面显示的文件的正确版本以防有人遇到类似的问题:
卡的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>
Firebase适配器的代码:
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);
}
};
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);
}
}