【问题标题】:Adding Google Maps to a RecyclerView将 Google 地图添加到 RecyclerView
【发布时间】:2018-07-06 11:41:59
【问题描述】:

我已经在RecyclerView 中添加了地图视图以及其他类型的列表项,但是现在......我如何以及在哪里初始化地图,我在哪里监听 onMapReady 以便我可以在之后放置一个标记,以及如何处理物品的回收?

任何想法在这种情况下的最佳做法是什么?

【问题讨论】:

    标签: android android-mapview android-maps-v2


    【解决方案1】:

    有两种可能的方法来做这件事,
    一个是Google Static Maps API using,它会给你地图的快照。

    另一个是,您可以在回收站项目中使用 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;
                }
    
            }
        }
    

    【讨论】:

    • 这个使用 googlemap v1,然后可以在 android 11 之后被删除并且不工作
    【解决方案2】:

    谷歌地图中有一种叫做精简模式的东西,你可以在回收站视图中使用

    检查这个litemode

    和示例代码示例LiteListDemoActivity

    【讨论】:

      【解决方案3】:

      例如,您可以使用Glide 并加载地图预览,而不是地图片段 像这样:

          GlideApp
              .with(context)
              .load("http://maps.google.com/maps/api/staticmap?center=" + 
                     lat + 
                     "," + 
                     lng + 
                     "&zoom=15&size=200x200&sensor=false" +
                     "&markers=color:red%7Clabel:C%" + 
                     markerLat + 
                     "," + 
                     markerLlng)
              .centerCrop()
              .into(myImageView);
      

      或者使用 lib - static-maps-api

      【讨论】:

      • 使用静态地图不是免费的,这个解决方案是个好主意,但要花一些钱。还是谢谢
      • @AlpAltunel 实际上,如您所见,我的代码不包含任何 api 密钥。因此,您可以根据需要多次使用它
      • 是的,但是如果您在一个请求后使用 like 您的代码,即使您使用密钥,它也会失败并给出 403 错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多