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) {}
});
}
描かれている方法。
努力は分かりますが、私の問題はカスタム情報ウィンドウを実装する方法ではなく、代わりに各マーカーに固有の情報ウィンドウを持つ方法です。 – Skemelio