2016-10-22 29 views
0

我正在製作一個應用程序,顯示地圖卡片視圖回收者視圖。最後,我得到了我想要的結果。但我有一些問題。我需要一些幫助來解決它Google Map Api,我必須在android recylcer適配器中進行初始化?

我不知道我要在哪裏初始化谷歌地圖。 - 所以選項和地圖位置不直接應用。 when app launch after scroll it one or twice

這是我的回收站適配器代碼

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; 
    } 
} 

在那裏我有初始化和設置位置來解決這個問題

回答

0

您可能希望通過調用MapsInitializer啓動。如文檔中所述,必須調用它,因爲需要初始化某些類,如BitmapDescriptorFactoryCameraUpdateFactory

但是,如果你正在使用MapFragmentMapView,並已通過調用這兩種類別的getMapAsync()並等待onMapReady(GoogleMap map)回調得到一個(非空)GoogleMap的,那麼你就不需要擔心這個類。

此外,如果你指的是Google Maps set in lite mode,可以按照以下方式:

  • 無論是作爲一個XML屬性爲MapViewMapFragment
  • 還是在GoogleMapOptions對象

您可以使用意圖啓動地圖視圖和生命週期事件,其中,如Google Maps Android API documentation

當使用完全交互模式中的API時,MapView類的用戶必須將所有活動生命週期方法轉發給MapView類中的相應方法。

當精簡版模式使用MapView類,轉發生命週期事件是可選的,除了以下情況:

  • 它是強制性調用onCreate(),否則沒有地圖將出現。
  • 如果您希望在lite模式地圖上顯示我的位置點並使用默認位置源,則需要撥打onResume()onPause(),因爲位置源只會在這些調用之間更新。如果您使用自己的位置來源,則無需調用這兩種方法。

您可能會發現到示例代碼here的鏈接。請致電SO post。它可能也有幫助。

相關問題