4 Aug 2015

Customizing info window contents in Google map v2 Android

In this article we will create an Android application that displays a customized info-window in GoogleMap Android API V2 using InfoWindowAdapter interface.

At first, Please follow this Article to implement Google map with your android application.

Once you have implemented Google map and its working fine with android application, let modify code to show custom info window of Google map.




1. Create info_window_layout.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:background="@android:color/white"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" />

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

2. Modify LoadingGoogleMap Method


Below is the code snippet need to change with previous article.

void LoadingGoogleMap(ArrayList<LatLngBean> arrayList)
       {     
              if (googleMap != null)
              {
                     googleMap.clear();
                     googleMap.getUiSettings().setMyLocationButtonEnabled(true);
                     googleMap.setMyLocationEnabled(true);
                     googleMap.getUiSettings().setZoomControlsEnabled(true);

                     if(arrayList.size()>0)
                     {                                
                           try
                           {                         
                                  listLatLng=new ArrayList<LatLng>();
                                  for (int i = 0; i < arrayList.size(); i++)
                                  {
                                         LatLngBean bean=arrayList.get(i);
                                         if(bean.getLatitude().length()>0 && bean.getLongitude().length()>0)
                                         {
                                                double lat=Double.parseDouble(bean.getLatitude());
                                                double lon=Double.parseDouble(bean.getLongitude());          

                                                Marker marker = googleMap.addMarker(new MarkerOptions()
                                                .position(new LatLng(lat,lon))
                                                .title(bean.getTitle())
                                                .snippet(bean.getSnippet())
                                                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

                                                //Add Marker to Hashmap
                                                hashMapMarker.put(marker,bean);

                                                //Set Zoom Level of Map pin
                                                LatLng object=new LatLng(lat, lon);
                                                listLatLng.add(object);
                                         }                                
                                  }
                                  SetZoomlevel(listLatLng);
                           }
                           catch (NumberFormatException e)
                           {
                                  e.printStackTrace();
                           }                         
                           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 marker)
                                  {                   
                                         // Getting view from the layout file info_window_layout
                                         View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);

                                         // Getting the position from the marker
                                         LatLngBean bean=hashMapMarker.get(marker);

                                         TextView tv_title = (TextView) v.findViewById(R.id.tv_title);
                                         TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
                                         TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);

                                         tv_title.setText("Title:" + bean.getTitle());
                                         tvLat.setText("Latitude:" + bean.getLatitude());
                                         tvLng.setText("Longitude:"+ bean.getLongitude());

                                         // Returning the view containing InfoWindow contents
                                         return v;

                                  }
                           });

                           googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

                                  @Override
                                  public void onInfoWindowClick(Marker marker)
                                  {
                                         LatLngBean bean=hashMapMarker.get(marker);
                                         Toast.makeText(getApplicationContext(), bean.getTitle(),Toast.LENGTH_SHORT).show();
                                  }
                           });
                     }
              }

              else
              {
                     Toast.makeText(getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
              }

       }

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)

2 comments: