问题描述
我已经将RecyclerView
内的地图视图与其他类型的列表项一起添加了,但是现在...如何以及在何处初始化地图,在哪里监听onMapReady以便以后可以放置标记,以及我该如何处理物品的回收?
I have added the map view inside the RecyclerView
alongside other types of list items but now ... how and where do I initialize the map, where do I listen for onMapReady so that I can place a marker afterwards, and how do I handle the recycling of the item ?
任何想法在这种情况下的最佳实践是什么?
Any ideas what the best practice is in this situation ?
推荐答案
有两个可能做到的事情,
一种是 Google Static Maps API 使用,它将为您提供快照地图
There are tow possibliy do this thing,
one is Google Static Maps API using, which will give you the snapshot of the map
另一种方法是,您可以在回收项目中使用com.google.android.gms.maps.MapView
并像下面的波纹管一样在viewholder
中进行初始化
Another is, you can use com.google.android.gms.maps.MapView
inside of recycler item and initialize in your viewholder
like bellow,
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;
}
}
}
这篇关于将Google Maps添加到RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!