如何在 google 地图应用中添加此自定义信息 window 而不是默认信息?

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

这是代码。我已经为 info window 创建了一个 custom adapter 应该替换默认的。我已经将地图设置为自定义适配器,但它仍然显示旧的。我是 java 的初学者。所以知道如何修改代码以添加自定义信息会很有帮助 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>

您正在 CustomInfoWindowAdaptergetInfoWindow 方法上返回 null

这是一个有效的实现:

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

考虑到根据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.

这意味着您添加到信息窗口的 Button 或可点击的 View 将不起作用。

如果您阅读文档,您会明白方法 getInfoContents() 应该 return 标记以便查看默认标记内容的变化,在您的情况下 getInfoWindow() 应该 return 创建的视图而不是 null 来覆盖默认标记。