2016-10-22 4 views
0

カードビューリサイクルビューで地図を表示しているアプリを作っています。最後に、私が望む結果が得られます。しかし、私はいくつかの問題があります。私はそれを解決するためにいくつかの助けが必要ですGoogleマップApi、どこで私はアンドロイドrecylcerアダプタで初期化する必要がありますか?

私はどこで私はGoogleマップを初期化する必要があります知りません。 - オプションと地図の場所は直接適用されません。ここで when app launch after scroll it one or twice

私リサイクラーアダプターコード私はあなたがMapsInitializerを呼び出すことによって開始することもできます初期化する必要がありますし、この問題

答えて

0

を解決するための場所を設定

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

です。ドキュメントで述べたように、BitmapDescriptorFactoryCameraUpdateFactoryなどのクラスを初期化する必要があるため、このメソッドを呼び出す必要があります。しかし

、あなたがMapFragmentMapViewを使用していて、すでにこれらのクラスのいずれかにgetMapAsync()を呼び出し、onMapReady(GoogleMap map)コールバックを待つことによって(非ヌル)Googleマップを取得した場合、あなたは、このクラスを心配する必要はありません。

あなたはGoogle Maps set in lite modeを参照している場合また、あなたはこれらの方法に従うことができます:

  • どちらかMapViewまたはMapFragment
  • それとも

GoogleMapOptionsオブジェクト内のXML属性としてインテントを使用して、マップビューとライフサイクルイベントを起動できます。Google Maps Android API documentation

完全対話モードでAPIを使用する場合、MapViewクラスのユーザーは、すべてのアクティビティライフサイクルメソッドをMapViewクラスの対応するメソッドに転送する必要があります。

ライフサイクルイベントを転送し、ライトモードでのMapViewクラス を使用して、次のような場合を除いて、オプションである:

  • そうでなければマップが表示されません、onCreate()を呼び出すことが必須です。
  • ライトのモードマップにマイロケーションのドットを表示し、デフォルトのロケーションソースを使用する場合は、ロケーションソースがこれらのコール間でのみ更新されるため、onResume()onPause()に電話する必要があります。独自のロケーションソースを使用する場合、これらの2つのメソッドを呼び出す必要はありません。

サンプルコードhereへのリンクがあります。

最後に、このSO postの解決策をご確認ください。それはまた助けるかもしれない。

関連する問題