我已经在 RecyclerView
中添加了 map View 以及其他类型的列表项,但是现在......我如何以及在哪里初始化 map ,我在哪里监听 onMapReady 以便我可以放置之后的标记,我该如何处理元素的回收?
知道在这种情况下最佳做法是什么吗?
最佳答案
有两种可能的方式来做这件事,
一个是 Google Static Maps API使用,这将为您提供 map 的快照。
另一个是,您可以在回收器项目中使用 com.google.android.gms.maps.MapView
并在您的 viewholder
中初始化,如下所示,
public class AdapterWithMap extends RecyclerView.Adapter<AdapterWithMap.CustomeHolder> {
@Override
public void onBindViewHolder(CustomeHolder holder, int position)
{
GoogleMap thisMap = holder.mapCurrent;
if(thisMap != null)
thisMap.moveCamera();//initialize your position with lat long or move camera
}
@Override
public void onViewRecycled(CustomeHolder holder)
{
// Cleanup MapView here?
if (holder.mapCurrent != null)
{
holder.mapCurrent.clear();
holder.mapCurrent.setMapType(GoogleMap.MAP_TYPE_NONE);
}
}
public class CustomeHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {
GoogleMap mapCurrent;
MapView map;
public CustomeHolder(View view) {
super(view);
map = (MapView) view.findViewById(R.id.mapImageView);
if (map != null)
{
map.onCreate(null);
map.onResume();
map.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getApplicationContext());
mapCurrent = googleMap;
}
}
}
关于android - 将 Google map 添加到 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51209686/