45

I want to make custom info window adapter in map v2 in android as like below.

enter image description here

I have seen below link but doesn't get more.

1,2,3,

below is my content layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
>
    <ImageView
        android:id="@+id/infocontent_iv_image"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentTop="true" 
    />
    <RelativeLayout
        android:id="@+id/infocontent_rl_middle"
        android:layout_below="@id/infocontent_iv_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_margin="5dp"
    >

    </RelativeLayout>
    <TextView
        android:id="@+id/infocontent_tv_name"
        android:layout_below="@id/infocontent_rl_middle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" 
        android:layout_margin="5dp"
    />
    <TextView
        android:id="@+id/infocontent_tv_type"
        android:layout_below="@id/infocontent_tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#CCCCCC"
        android:layout_margin="5dp"
    />
    <TextView
        android:id="@+id/infocontent_tv_desc"
        android:layout_below="@id/infocontent_tv_type"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
    />
    <TextView
        android:id="@+id/infocontent_tv_addr"
        android:layout_below="@id/infocontent_tv_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
    />
</RelativeLayout>

So any one help me how can i set data to all views in infowindow adapter?

Community
  • 1
  • 1
Girish Bhutiya
  • 3,111
  • 5
  • 31
  • 50

2 Answers2

73

Try this

windowlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_lat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_lng"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

public class MainActivity extends FragmentActivity {


GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Getting reference to the SupportMapFragment of activity_main.xml
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting GoogleMap object from the fragment
    googleMap = mapFragment.getMap();

    // Setting a custom info window adapter for the google map
    googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        // Use default InfoWindow frame
        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        // Defines the contents of the InfoWindow
        @Override
        public View getInfoContents(Marker arg0) {

            // Getting view from the layout file info_window_layout
            View v = getLayoutInflater().inflate(R.layout.windowlayout, null);

            // Getting the position from the marker
            LatLng latLng = arg0.getPosition();

            // Getting reference to the TextView to set latitude
            TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);

            // Getting reference to the TextView to set longitude
            TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);

            // Setting the latitude
            tvLat.setText("Latitude:" + latLng.latitude);

            // Setting the longitude
            tvLng.setText("Longitude:"+ latLng.longitude);

            // Returning the view containing InfoWindow contents
            return v;

        }
    });

    // Adding and showing marker while touching the GoogleMap
    googleMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng arg0) {
            // Clears any existing markers from the GoogleMap
            googleMap.clear();

            // Creating an instance of MarkerOptions to set position
            MarkerOptions markerOptions = new MarkerOptions();

            // Setting position on the MarkerOptions
            markerOptions.position(arg0);

            // Animating to the currently touched position
            googleMap.animateCamera(CameraUpdateFactory.newLatLng(arg0));

            // Adding marker on the GoogleMap
            Marker marker = googleMap.addMarker(markerOptions);

            // Showing InfoWindow on the GoogleMap
            marker.showInfoWindow();

        }
    });

}
}

In this way you can create custom Layout and achieve the way you want to do.

Let me know if it helps you.

moDev
  • 5,248
  • 4
  • 33
  • 63
  • I have add my layout file of infowindow adapter. So can you help me how can i set data to all views? – Girish Bhutiya Feb 27 '13 at 04:49
  • What do you mean by all views?? View will be one,data will be different?? – moDev Feb 27 '13 at 07:50
  • ya but I have 4 textview and one imageview so can i set value to all. – Girish Bhutiya Feb 27 '13 at 07:54
  • through this method i will get only two string(title & snippet) but how can i set value to other textviews & imageView and image view has dynamic image according to marker clicked. – Girish Bhutiya Feb 27 '13 at 08:44
  • 1
    Just need to apply the logic. Take a loop and add markers along with the position and same way to show the contents – moDev Feb 27 '13 at 09:02
  • 1
    look at this answer http://stackoverflow.com/questions/13713726/maps-api-v2-with-different-marker-actions – LKallipo Feb 27 '13 at 15:38
  • Thanks it work fine. But do you know how can i add buttons in infowindow? – Girish Bhutiya Mar 01 '13 at 09:52
  • @droiddev i have two pin for different location, for one pin i set custom infowindow and for other pin i want to set default infowindow. is it possible? – Hiren Patel Jul 15 '13 at 07:29
  • Perfect decision! Thank You! – Konstantin.Efimenko Aug 23 '13 at 11:18
  • But if I put the buttons inside the infowindow then the buttons wont accept the clicks. Buttons wont be live. Do you have any solution for that? – Code Word Sep 17 '14 at 01:37
  • @CodeWord Have a look at this https://developers.google.com/maps/documentation/android/marker#marker_click_events – moDev Sep 17 '14 at 08:36
  • Can I add a custom info window to the map marker and not the whole map? Associating the custom adapter with the map and not the map marker seems counter-intuitive. – Andrew S Apr 12 '15 at 20:01
  • @AndrewS didn't get it – moDev Apr 12 '15 at 20:02
  • @droid_dev: Here you add your custom addapter to the google map with googleMap.setInfoWindowAdapter(new InfoWindowAdapter()......). I have created a separate implementation of the InfoWindowAdapter interface as it's own file. I don't want to set this interface on the whole map as you have done but on each map marker. That way different map markers can have different adapters and this different functionality. – Andrew S Apr 12 '15 at 20:13
  • use loop to add different marker and its info window – moDev Apr 12 '15 at 20:16
  • see this for better idea http://www.rogcg.com/blog/2014/04/20/android-working-with-google-maps-v2-and-custom-markers – moDev Apr 12 '15 at 20:17
  • I do not get how a loop will help. I saw that example, but a map can have only one InfoWindowAdapter. I think I've just worked it out, you do not need a loop at all. The methods of the interface have the marker they are associated with as a parameter. It's this that can be used to dynamically choose a layout in the adapter. Thanks for replying so fast. – Andrew S Apr 12 '15 at 20:20
  • error for the line googleMap = mapFragment.getMap(); Can't resolve getMap() –  Mar 31 '18 at 05:05
22

I have open dialog onMarker() click. and setContentView(my layout).

I have use below code.

@Override
    public boolean onMarkerClick(Marker arg0) {
        if(arg0.getSnippet() == null){
            mMap.moveCamera(CameraUpdateFactory.zoomIn());
            return true;
        }
        //arg0.showInfoWindow();
        final DataClass data = myMapData.get(arg0);
        final Dialog d = new Dialog(MainActivity.this);
        d.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //d.setTitle("Select");
        d.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        d.setContentView(R.layout.info_content);
        ivPhoto = (ImageView)d.findViewById(R.id.infocontent_iv_image);
        AddImageOnWindow executeDownload = new AddImageOnWindow();
        final LatLng l = arg0.getPosition();
        executeDownload.execute(l);
        TextView tvName = (TextView)d.findViewById(R.id.infocontent_tv_name);
        tvName.setText(data.getPlaceName());

        TextView tvType = (TextView)d.findViewById(R.id.infocontent_tv_type);
        tvType.setText("("+data.getPlaceType()+")");

        TextView tvDesc = (TextView)d.findViewById(R.id.infocontent_tv_desc);
        tvDesc.setText(data.getPlaceDesc());

        TextView tvAddr = (TextView)d.findViewById(R.id.infocontent_tv_addr);
        tvAddr.setText(Html.fromHtml(data.getPlaceAddr()));

d.show();
return true;
Girish Bhutiya
  • 3,111
  • 5
  • 31
  • 50