Reputation: 149
Hi I´m working with google maps and I create a custom title on the markers that I have, I need to know when a marker is selected how do I get the info on the title, becouse I need to open a fragment with that info, letme paste you the code I got:
@Override
public void onMapReady(GoogleMap googleMap) {
map =googleMap;
map.getUiSettings().setZoomControlsEnabled(true);
for (Taxi taxi : taxis) {
u= taxi.getUbicacionActual();
LatLng ubicacion= new LatLng(Double.parseDouble(u.getLatitud()), Double.parseDouble(u.getLongitud()));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(ubicacion,15));
MarkerOptions punto= new MarkerOptions().position(ubicacion);
map.addMarker(punto);
map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View v = getLayoutInflater().inflate(R.layout.info_title, null);
TextView info1 = (TextView) v.findViewById(R.id.info1);
TextView info2 = (TextView) v.findViewById(R.id.info2);
TextView info3 = (TextView) v.findViewById(R.id.info3);
info1.setText("Fecha: " + u.getFecha());
info3.setText("Longitud: " + u.getLongitud().toString());
info2.setText("Ubicacion: "+obtenerDireccion(u.getLatitud(),u.getLongitud()));
return v;
}
});
}
map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Fragment fragmento;
fragmento = new HistorialFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_principal, fragmento)
.commit();
}
});
On the method OnInfoWindowClickListener where I called the new fragment I need to send the info from the custom title, could someone please help me with this?
Upvotes: 1
Views: 3098
Reputation: 23881
try this:
Add your setOnInfoWindowClickListener
inside getInfoContents(Marker marker)
method:
map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View v = getLayoutInflater().inflate(R.layout.info_title, null);
final TextView info1 = (TextView) v.findViewById(R.id.info1);
TextView info2 = (TextView) v.findViewById(R.id.info2);
TextView info3 = (TextView) v.findViewById(R.id.info3);
info1.setText("Fecha: " + u.getFecha());
info3.setText("Longitud: " + u.getLongitud().toString());
info2.setText("Ubicacion: "+obtenerDireccion(u.getLatitud(),u.getLongitud()));
map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
public void onInfoWindowClick(Marker marker)
{
Fragment fragmento;
fragmento = new HistorialFragment();
Bundle bundle = new Bundle();
bundle.putString("title",info1.getText().toString());
fragmento.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_principal, fragmento)
.commit();
}
});
return v;
}
});
Upvotes: 2
Reputation: 18242
If you set the title of the Marker
doing
MarkerOptions punto= new MarkerOptions()
.position(ubicacion)
.title("Fecha: " + u.getFecha()); // set the marker title
map.addMarker(punto);
You can retrieve it on the onInfoWindowClick
method doing:
map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
String title = marker.getTitle(); // Retrieve the title
}
});
Upvotes: 0