18

I have seen that people are finding a lot of difficulties with this specific task and I am in need of help as well.

I have successfully created Markers on a Map using the default code that Google has provided. But now I want to be able to click on "InfoWindow" to open a new activity so I can add more information.

Does anyone know the best way to do this?

If you can answer this please put up some code or an example. Any help would be much appreciated!

Andrés Pérez-Albela H.
  • 4,003
  • 1
  • 18
  • 29
user1977908
  • 1,105
  • 4
  • 11
  • 16

2 Answers2

55

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);


            }
        });
Quickcoding
  • 735
  • 8
  • 14
  • Thank you it worked! I will post some example code for other users! – user1977908 Jun 01 '13 at 19:56
  • what is Mymap? Mine cannot resolve symbol 'setOnInfoWindowClickListener' because Mymap is not correct for my code... Is it the instance of the GoogleMap class? Is it the Class for the Activity? Or is it a new separate class inheriting from GoogleMap class? – Pat Myron Dec 23 '15 at 00:44
  • @AlexVPerl Hi it maybe odd, but I use the title with a json string. Then you need to use a custom infowindow adapter to correctly display your content. Something like {"title":"Your Title","param1":"yourParam}. –  Dec 20 '16 at 23:11
1
This method works even well with multiple markers. get the title of the marker using marker.getTitle() and Starts the activity based on which marker you clicked. 

public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        // Add a marker in Sydney and move the camera
        LatLng chennai = new LatLng(12.9671, 80.2593);
        mMap.addMarker(new MarkerOptions().position(chennai).title("Chennai"));

        LatLng perungudi = new LatLng(12.97, 80.25);
        mMap.addMarker(new MarkerOptions().position(perungudi).title("Perungudi"));

        LatLng pallikarnai = new LatLng(12.9377, 80.2154);
        mMap.addMarker(new MarkerOptions().position(pallikarnai).title("Pallikarnai"));

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(chennai,12));
        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                if (marker.getTitle().equals("Chennai")){
                    Toast.makeText(MapsActivity.this, "Clicked"+marker.getTitle(), Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });

    }
Jarin Rocks
  • 975
  • 10
  • 17