Android Google Maps, how to make each Marker InfoWindow open different Activity?
Use a HashMap to store the marker ID and it's corresponding identification of which Activity it should open.
Then, use a OnInfoWindowClickListener
to get the event of a user clicking the info window, and use the HashMap to determine which Activity to open.
Declare the HashMap as an instance variable:
//Declare HashMap to store mapping of marker to Activity
HashMap<String, String> markerMap = new HashMap<String, String>();
Then, each time you add a Marker, make an entry in the HashMap:
String id = null;
Marker a1 = googleMap.addMarker(new MarkerOptions().position(a)
.title(arr[0])
.snippet(arr[1]));
id = a1.getId();
markerMap.put(id, "a1");
Marker b1 = googleMap.addMarker(new MarkerOptions().position(b)
.title(arr[9])
.snippet(arr[10]));
id = b1.getId();
markerMap.put(id, "b1");
Marker c1= googleMap.addMarker(new MarkerOptions().position(c)
.title(arr[18])
.snippet(arr[19]));
id = c1.getId();
markerMap.put(id, "c1");
Marker d1= googleMap.addMarker(new MarkerOptions().position(d)
.title(arr[27])
.snippet(arr[28]));
id = d1.getId();
markerMap.put(id, "d1");
}
And then define the info window click listener:
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
String m = markerMap.get(marker.getId());
if (m.equals("a1")){
Intent i = new Intent(MainActivity.this, ActivityA1.class);
startActivity(i);
}
else if (m.equals("b1")){
Intent i = new Intent(MainActivity.this, ActivityB1.class);
startActivity(i);
}
else if (m.equals("c1")){
Intent i = new Intent(MainActivity.this, ActivityC1.class);
startActivity(i);
}
else if (m.equals("d1")){
Intent i = new Intent(MainActivity.this, ActivityD1.class);
startActivity(i);
}
}
});
Can I launch other activites from different markers on Google Maps?
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);
Android google maps markers with different values on Info Window
Use a HashMap
I'm not sure if this is the best solution but it works. Basically I just added the marker into a HashMap and passed this HashMap to my Custom Info Window class.
Adding the markers:
HashMap<Marker, JSONObject> stopsMarkersInfo = new HashMap<>(); // created the HashMap
JSONArray stops = new File().getStops(lineId);
for (int i = 0; i < stops.length(); i++) {
BitmapDescriptor stopMarkerIcon = new File().getStopMarkerIcon(color, Integer.parseInt(stops.getJSONObject(i).getString("sequence")));
LatLng coordinate = new LatLng(Double.parseDouble(stops.getJSONObject(i).getString("lat")), Double.parseDouble(stops.getJSONObject(i).getString("lng")));
MarkerOptions markerOptions = new MarkerOptions().icon(stopMarkerIcon).position(coordinate).anchor(.5f, .5f);
Marker marker = googleMap.addMarker(markerOptions);
stopsMarkersInfo.put(marker, stops.getJSONObject(i)); // added each marker and
// his information to the HashMap
}
googleMap.setInfoWindowAdapter(new StopsInfoWindow(stopsMarkersInfo)); // passed the HashMap
Info Window Adapter:
public class StopsInfoWindow implements GoogleMap.InfoWindowAdapter {
private HashMap<Marker, JSONObject> stopsMarkersInfo;
private View view;
public StopsInfoWindow(HashMap<Marker, JSONObject> stopsMarkersInfo) {
this.stopsMarkersInfo = stopsMarkersInfo;
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
@Override
public View getInfoWindow(final Marker marker) {
JSONObject stop = stopsMarkersInfo.get(marker);
if (stop != null) {
LayoutInflater inflater = (LayoutInflater) Controller.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item_stop_marker_info, null);
TextView stopName = (TextView) view.findViewById(R.id.stop_name);
stopName.setText(stop.getString("name"));
TextView stopLine = (TextView) view.findViewById(R.id.stop_line);
stopLine.setText(stop.getString("line"));
}
return view;
}
}
Android Google Map v2 - Starting activity when clicking on marker infoWindow
add this to your code
Mymap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(MapActivity.this,OtherActivity.class);
startActivity(intent);
}
});
How to open different activities depending OnInfoWindowClick
you are checking like this in your if clause:
if (googleMap.equals("Durban")) {
Intent intent = new Intent(MainActivity.this, Durban.class);
startActivity(intent);
}
but you should check on the marker of the public void onInfoWindowClick(Marker marker)
method.
do something like this:
if (marker.getTitle().equals("Durban")) {
Intent intent = new Intent(MainActivity.this, Durban.class);
startActivity(intent);
}
Sending data through InfoWindow to another activity Android
Try this way, You will get different value on InfoWindow Click
if your bean (details
) have different value.
for (final infoToStore details : info) {
marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble(details.getLat()), Double.parseDouble(details.getLng())))
.title(details.getName())
.snippet(details.getDesc()));
// Attach your bean with marker
marker.setTag(details);
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
//Get Attached bean from marker
infoToStore infoAttached = ((infoToStore) marker.getTag());
Intent intent = new Intent(MainActivity.this, InfoWindow.class);
intent.putExtra(NAME, infoAttached.getName());
intent.putExtra(DESCRIPTION, infoAttached.getDesc());
intent.putExtra(CATEGORY, infoAttached.getCat());
intent.putExtra(IMAGE_URL, infoAttached.getUrl());
intent.putExtra(PUSH_ID, infoAttached.getPushID());
startActivity(intent);
}
});
}
Related Topics
Drawable-Hdpi, Drawable-Mdpi, Drawable-Ldpi Android
Why Was Actionbaractivity Deprecated
How to Get Multiple Icons to Launch Different Activities in One Application
Android Slidingdrawer from Top
Passing Touch Events to the Parent View
Drawable Folders in Res Folder
Android Play Services 6.5: Locationclient Is Missing
Android Path to Asset Txt File
Android Device Gps On/Off Programmatically
How to Make a Actionbar Like Google Play That Fades in When Scrolling
Avoid Button Multiple Rapid Clicks
Expandable List with Recyclerview
Android Permissions: Phone Calls: Read Phone State and Identity
Add Custom Font for Complete Android Application
How to Display Activity When the Screen Is Locked
How to Use Holo.Light Theme, and Fall Back to 'Light' on Pre-Honeycomb Devices