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>
You are returning
null
on thegetInfoWindow
method of yourCustomInfoWindowAdapter
.Here is a working implementation:
Take into account that according to the documentation:
This means that the
Button
s or clickableView
s that you add to the InfoWindow will not work.