kocsisur kocsisur - 13 days ago 6
Android Question

Can I launch other activites from different markers on Google Maps?

I have a main activity and on that i have some markers ( too much to initialize them one by one )
When i click on the infowidow i want to open a fragment with the specific marker.
Currently i have something like this

for( i = 0; i<j; i++)
{
hely = helyek.get(i);
if(hely.getStars()>=2){
hely.getHelyId();
Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(hely.getLat(),hely.getLng()))
.title(hely.getHelynev())
.snippet(hely.getHelycim())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
markers.add(marker);


This adds the markers to the Map.

public void onMapReady(GoogleMap googleMap) {
int j;
int i=0;
List<Marker> markers = new ArrayList<Marker>();
mMap = googleMap;
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(getApplicationContext(), HelyDetailActivity.class);
intent.putExtra(ListActivity.HELY_NEV_EXTRA, hely.getHelynev());
intent.putExtra(ListActivity.HELY_CIM_EXTRA, hely.getHelycim());
intent.putExtra(ListActivity.HELY_JOVALASZOK, hely.getJovalaszok());
intent.putExtra(ListActivity.HELY_OSSZKERDES, hely.getOsszkerdes());
intent.putExtra(ListActivity.HELY_ID_EXTRA, hely.getHelyId());
intent.putExtra(ListActivity.HELY_LAT,hely.getLat());
intent.putExtra(ListActivity.HELY_LONG, hely.getLng());
startActivity(intent);
}
});
mMap.setMyLocationEnabled(true);
}


This opens an activity of coures but the same one every time. How can i define which marker I clicked?

Answer

You can try using declaring a new HashMap data structure. When you're adding the markers to the map, you can put the Hely object into the map like so:

HashMap<String, Hely> helyMap = new HashMap<>(); //... code to create the marker ...// markers.add(marker); helyMap.put(marker.getId(), hely);

Then, in the onInfoWindowClick(Marker marker) method retrieve the Hely object based on the marker that was clicked like so:

Hely hely = helyMap.get(marker.getId()); Intent intent = new Intent(getApplicationContext(), HelyDetailActivity.class); intent.putExtra(ListActivity.HELY_NEV_EXTRA, hely.getHelynev()); // ... other intent code ... // startActivity(intent);

Comments