2017-08-11 18 views
1

Google Mapsを使用していくつかのマーカーを表示するためにMapsActivityが含まれるAndroidアプリケーションがあります。タイムスタンプを使用して作成されているGoogleマップ:マーカーごとに異なるCustomInfowindow

マーカーは

TimestampオブジェクトがFirebaseデータベースに保存されている

(double lat,lon; 
    int stepSum; 
    long timeMilli; 
    String state=null;) 

属性オブジェクト。

私はデータベースから各タイムスタンプを取り出し、上のアトリビュートでマーカーを作成しようとします。私の問題は、マーカーをクリックするとカスタム情報ウィンドウが表示されますが、はすべてのマーカーで同じです。マーカーによって異なる属性が表示されるはずです。

なぜこれが私が新しいマーカーを作成し、mMap.setInfoWindowAdapter(infoWindow);で のGoogleMapオブジェクトへの情報ウィンドウを設定していたとき、私は別の情報ウィンドウをインスタンス化drawMarkers()方法で

が起きています。

結果として、mMap.setInfoWindowAdapter(infoWindow);はマーカーが作成されて何回も呼び出され、最後にインフォウィンドウだけが存続します。それは問題ですが、私は解決策を見つけられません。

マーカーをクリックすると何らかのデータが表示され、別のマーカーをクリックすると別の種類のデータ(同じレイアウト、異なる属性)が表示されます。

また、有効な例もあります。

CustomInfoWindowクラスは

private class CustomInfoWindow implements GoogleMap.InfoWindowAdapter{ 

    private String title=null,hour=null,state=null; 
    private int steps=0; 

    public CustomInfoWindow(){} 

    public CustomInfoWindow(String title, String hour, String state, int steps) { 
     this.title = title; 
     this.hour = hour; 
     this.state = state; 
     this.steps = steps; 
    } 

    @Override 
    public View getInfoWindow(Marker marker) { 
     return null; 
    } 

    public void setState(String state) { 
     this.state = state; 
    } 

    @Override 
    public View getInfoContents(Marker marker) { 
     LayoutInflater inflater = LayoutInflater.from(getApplicationContext()); 
     View root = inflater.inflate(R.layout.marker_infowindow,null); 
     TextView titleTextView = (TextView) root.findViewById(R.id.infowindow_title); 
     TextView hourTextView = (TextView) root.findViewById(R.id.infowindow_hour); 
     TextView stepsTextView = (TextView) root.findViewById(R.id.infowindow_steps); 
     TextView stateTextView = (TextView) root.findViewById(R.id.infowindow_state); 
     titleTextView.setText(title); 
     if (state!=null){ 
      stateTextView.setText(getApplicationContext().getString(R.string.state)+" "+state); 
      stateTextView.setVisibility(View.VISIBLE); 
     } 
     hourTextView.setText(getApplicationContext().getString(R.string.time)+" "+hour); 
     stepsTextView.setText(getApplicationContext().getString(R.string.steps_so__far)+" "+steps); 
     return root; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public void setHour(String hour) { 
     this.hour = hour; 
    } 

    public void setSteps(int steps) { 
     this.steps = steps; 
    } 
} 

マーカーは、私が情報ウィンドウについてgoogle's tutorialを読みましたが、私の問題を解決することができませんでした

private void drawMarkers(Calendar c){ 
    String userId = this.user.getAccount().getId(); 
    int year = c.get(Calendar.YEAR); 
    int month = c.get(Calendar.MONTH); 
    int dayOfMonth = c.get(Calendar.DAY_OF_MONTH); 

    final DatabaseReference timestampRef = FirebaseDatabase.getInstance().getReference() 
      .child(FirebaseConstant.TIMESTAMPS.toString()); 
    timestampRef.child(userId).child(""+year).child(""+month).child(""+dayOfMonth) 
      .addListenerForSingleValueEvent(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot dataSnapshot) { 
        long childCounter=0; 
        for (DataSnapshot timestampSnapshot:dataSnapshot.getChildren()){ 
         CustomInfoWindow infoWindow=new CustomInfoWindow(); 
         Timestamp temp = timestampSnapshot.getValue(Timestamp.class); 
         if (temp!=null){ 
          infoWindow.setTitle(getString(R.string.todays_timestamp)); 
          infoWindow.setHour(getFormatedHourFromTimeMilli(temp.getTimeMilli())); 
          infoWindow.setSteps(temp.getStepSum()); 
          if (temp.getState()!=null){ 
           infoWindow.setState(temp.getState()); 
          } 
          mMap.setInfoWindowAdapter(infoWindow); 
          drawTimestamp(temp); 
          infoWindow=null; 
         } 
        } 
       } 

       @Override 
       public void onCancelled(DatabaseError databaseError) {} 
      }); 
} 

描かれている方法。

答えて

0

私のソリューション

  • GoogleMap.InfoWindowAdapterを拡張するクラスを作成します。これは、マップ全体のカスタム情報ウィンドウになります。
  • onMapReady()の内部では、customInfoWindowオブジェクトの実装をGoogleMapオブジェクトに設定します。これは、ユーザーが地図からマーカーをクリックしたときに表示される情報ウィンドウです。
  • 最後に、onMapReady()メソッドの内部でOnMarkerClickListenerをGoogleMapオブジェクトに設定します。 GoogleMap.OnMarkerClickListenerの実装。onMarkerClick(マーカーマーカー)のみは、どのマーカーがクリックされたかに応じて、情報ウィンドウのコンテンツ(インフォウィンドウオブジェクトの属性を変更し、必要に応じて呼び出す)を変更します。
1

希望すると、exampleは単純なxmlレイアウトとして使用するのに役立ちます。 覚えているボタンは機能しません。テキストビューは、実行可能な状態でボタンを取得したい場合は、その場で要件を完了します。chose007 exampleに従ってください。

+0

努力は分かりますが、私の問題はカスタム情報ウィンドウを実装する方法ではなく、代わりに各マーカーに固有の情報ウィンドウを持つ方法です。 – Skemelio

関連する問題