谷歌地图Api,我必须在android recylcer适配器中初始化吗?
我正在制作一个应用程序,在卡片视图中显示地图,并在回收器视图中显示地图。最后我得到了我想要的结果。但是我有一些问题。我需要一些帮助来解决它 我不知道在哪里我必须初始化谷歌地图。 -因此,选项和地图位置不能直接应用。 这是我的回收器适配器代码谷歌地图Api,我必须在android recylcer适配器中初始化吗?,android,google-maps,android-recyclerview,Android,Google Maps,Android Recyclerview,我正在制作一个应用程序,在卡片视图中显示地图,并在回收器视图中显示地图。最后我得到了我想要的结果。但是我有一些问题。我需要一些帮助来解决它 我不知道在哪里我必须初始化谷歌地图。 -因此,选项和地图位置不能直接应用。 这是我的回收器适配器代码 public class EqListAdapter extends RecyclerView.Adapter<EqListAdapter.MyViewHolder>{ Context mContext; EqData eqData; View
public class EqListAdapter extends RecyclerView.Adapter<EqListAdapter.MyViewHolder>{
Context mContext;
EqData eqData;
View convertView;
public EqListAdapter(Context mContext, EqData eqData) {
this.mContext = mContext;
this.eqData = eqData;
}
@Override
public EqListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(EqListAdapter.MyViewHolder holder, int position) {
final float str = eqData.result.get(position).strength;
final LatLng latlng = new LatLng(eqData.result.get(position).lat,eqData.result.get(position).lon);
final String loc = eqData.result.get(position).location;
final String time = eqData.result.get(position).date;
GoogleMap thismap = holder.gMap;
if (thismap != null){
thismap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 13));
thismap.addMarker(new MarkerOptions().position(latlng));
thismap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
}
});
thismap.getUiSettings().setMapToolbarEnabled(false);
}
holder.locationText.setText(loc);
holder.strText.setText(""+str);
holder.timeText.setText(time);
}
@Override
public void onViewRecycled(MyViewHolder holder) {
super.onViewRecycled(holder);
// Cleanup MapView here?
/* if (holder.gMap != null)
{
holder.gMap.clear();
holder.gMap.setMapType(GoogleMap.MAP_TYPE_NONE);
}
}*/
@Override
public int getItemCount() {
return this.eqData.result.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback
{
TextView strText;
TextView locationText;
TextView timeText;
MapView map;
GoogleMap gMap;
MyViewHolder(View itemView) {
super(itemView);
map = (MapView)itemView.findViewById(R.id.mapFragment);
strText = (TextView)itemView.findViewById(R.id.strText);
locationText = (TextView)itemView.findViewById(R.id.locationText);
timeText = (TextView)itemView.findViewById(R.id.timeText);
if (map != null)
{
map.onCreate(null);
map.onResume();
map.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(mContext);
gMap = googleMap;
}
}
公共类EqListAdapter扩展了RecyclerView.Adapter{
语境;
EqData EqData;
视图转换视图;
公共eqlistapter(上下文mContext、EqData、EqData){
this.mContext=mContext;
this.eqData=eqData;
}
@凌驾
public EqListAdapter.MyViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图v=LayoutFlater.from(mContext)。充气(R.layout.list\u项,父项,false);
返回新的MyViewHolder(v);
}
@凌驾
BindViewHolder上的公共无效(EqListAdapter.MyViewHolder,int位置){
最终浮点str=eqData.result.get(position).str;
最终车床车床=新车床(eqData.result.get(position).lat,eqData.result.get(position).lon);
最终字符串loc=eqData.result.get(position.location);
最终字符串时间=eqData.result.get(position).date;
谷歌地图thismap=holder.gMap;
if(thismap!=null){
此地图。移动相机(CameraUpdateFactory.newLatLngZoom(latlng,13));
thismap.addMarker(新的MarkerOptions().position(latlng));
thismap.setOnMapClickListener(新的GoogleMap.OnMapClickListener(){
@凌驾
公共无效的点击(LatLng LatLng){
}
});
thismap.getUiSettings().setMapToolbarEnabled(false);
}
holder.locationText.setText(loc);
holder.strText.setText(“+str”);
holder.timeText.setText(时间);
}
@凌驾
视图上的公共无效已回收(MyViewHolder){
super.onViewRecycled(支架);
//这里是地图视图吗?
/*如果(holder.gMap!=null)
{
holder.gMap.clear();
holder.gMap.setMapType(GoogleMap.MAP\u TYPE\u无);
}
}*/
@凌驾
public int getItemCount(){
返回此.eqData.result.size();
}
类MyViewHolder扩展了RecyclerView.ViewHolder在MapReadyCallback上实现
{
文本视图strText;
文本视图位置文本;
文本查看时间文本;
地图视图;
谷歌地图;
MyViewHolder(查看项目视图){
超级(项目视图);
map=(MapView)itemView.findViewById(R.id.mapFragment);
strText=(TextView)itemView.findViewById(R.id.strText);
locationText=(TextView)itemView.findViewById(R.id.locationText);
timeText=(TextView)itemView.findViewById(R.id.timeText);
if(map!=null)
{
map.onCreate(null);
onResume();
getMapAsync(this);
}
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
初始化(mContext);
gMap=谷歌地图;
}
}
如果我必须初始化并设置位置以解决此问题,您可能需要先调用。如文档中所述,必须调用它,因为需要初始化某些类,例如
BitmapDescriptorFactory
和CameraUpdateFactory
但是,如果您正在使用MapFragment
或MapView
,并且已经通过对这些类中的任何一个调用getMapAsync()
并等待onMapReady(GoogleMap)
回调来获得(非空)GoogleMap,那么您就不必担心这个类
此外,如果您指的是,您可以按照以下方式操作:
- 作为
或MapView
MapFragment的XML属性
- 或者在
对象中谷歌地图选项
MapView
类的用户必须将所有活动生命周期方法转发到MapView
类中的相应方法
在lite模式下使用MapView类时,转发生命周期事件是可选的,但以下情况除外:
- 必须调用
,否则将不显示映射onCreate()
- 如果希望在lite模式映射上显示“我的位置”点并使用默认位置源,则需要调用
和onResume()
,因为位置源只会在这些调用之间更新。如果使用自己的位置源,则无需调用这两个方法onPause()
最后,请检查此中的解决方案。它可能也会有所帮助。您可能希望从调用开始。如文档中所述,必须调用它,因为需要初始化某些类,如
BitmapDescriptorFactory
和CameraUpdateFactory
但是,如果您正在使用MapFragment
或MapView
,并且已经通过对这些类中的任何一个调用getMapAsync()
并等待onMapReady(GoogleMap)
回调来获得(非空)GoogleMap,那么您就不必担心这个类
此外,如果您指的是,您可以按照以下方式操作:
- 作为
或MapView
MapFragment的XML属性
- 或者在
对象中谷歌地图选项
MapView
类的用户必须将所有活动生命周期方法转发到MapView
类中的相应方法
在lite模式下使用MapView类时,可以选择转发生命周期事件