How can I add this custom info window in google maps app instead of the default one?

490 views Asked by At

This is code. I have created a custom adapter for info window that should replace the default one. I've set the map to custom adapter but it's still showing the old one. I'm a beginner in java. So it would be helpful to know how to modify the code to add the custom info window

MapsActivity

package com.example.nobodyme.buildingareafinder;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;


public class MapsActivity extends FragmentActivity implementOnMapReadyCallback {

private GoogleMap mMap;
private int i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_maps);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map1);
    mapFragment.getMapAsync(this);


}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;


    //setting up custom info window adapter to map
    mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());


    //Long click listener for placing marker on map
    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick(LatLng latLng) {
            Marker marker = mMap.addMarker(new MarkerOptions()
                    .position(latLng)
                    .visible(true)
                    .draggable(true)
                    .title("Marker " + i)
                    .snippet("Point added " + i));


            marker.setDraggable(true);

            Toast.makeText(MapsActivity.this, latLng.toString(), Toast.LENGTH_LONG).show();
            i++;
        }
    });


    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            marker.remove();
        }
    });


}


// class for custominfoadapter

public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    private View view;

    //to display heading distance
    private String title_distance = getResources().getString(R.string.distance);

    //to display distance from other markers
    private String distance_no;

    //to display snippet from original adpater
    private String custom_snippet;

    public CustomInfoWindowAdapter() {
        view = getLayoutInflater().inflate(R.layout.custom_info_window,
                null);
    }

    @Override
    public View getInfoContents(Marker marker) {

        distance_no = marker.getTitle();
        custom_snippet = marker.getSnippet();

        return null;
    }


    @Override
    public View getInfoWindow(final Marker marker) {


        return null;
    }


}

}

custom_info_window.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/background_light"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="14dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ff0000"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/snippet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ff7f7f7f"
        android:textSize="14dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_delete" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/custom_marker_remove"
            android:gravity="center"
            android:textColor="#ff7f7f7f"
            android:textSize="14dp"
            android:layout_alignParentTop="true" />
    </LinearLayout>


</LinearLayout>
2

There are 2 answers

0
antonio On BEST ANSWER

You are returning null on the getInfoWindow method of your CustomInfoWindowAdapter.

Here is a working implementation:

public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    @Override
    public View getInfoWindow(final Marker marker) {
        View v = getLayoutInflater().inflate(R.layout.custom_info_window, null);

        TextView title = (TextView)v.findViewById(R.id.title);
        title.setText(marker.getTitle());

        TextView snippet = (TextView)v.findViewById(R.id.snippet);
        snippet.setText(marker.getSnippet());

        return v;
    }
}

Take into account that according to the documentation:

The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (for example, after an image has loaded), call showInfoWindow(). Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.

This means that the Buttons or clickable Views that you add to the InfoWindow will not work.

0
Pb Vignesh On

If you read the documentation you would understand that the method getInfoContents() should return the marker inorder to see a change in the contents of the default marker and in your case the getInfoWindow() should return the view that was created instead of null to override the default marker.