标记自定义信息窗口图像视图覆盖所有其他图像

Marker CustomInfoWindow Imageview overrides all other images

我正在尝试制作带有一些标记的地图,它应该有一个带有图像和文本视图的信息标签。我已经解决了每个信息标签的文本不同的问题,但我正在为图像视图而苦苦挣扎。

当我添加新标记时,我的应用程序会获取新图像并将其放入存在的每个信息窗口中...

这是我的代码片段,我在其中设置图像和文本视图的值:

public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    private Activity context;

    public CustomInfoWindowAdapter(Activity context){
        this.context = context;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        boolean imageGeandert = false;
        View view = context.getLayoutInflater().inflate(R.layout.custom_infowindow, null);

        TextView tvTitle = (TextView) view.findViewById(R.id.nameTxt);
        TextView tvSubTitle = (TextView) view.findViewById(R.id.addressTxt);


        tvTitle.setText(marker.getTitle());
        tvSubTitle.setText(marker.getSnippet());

            ImageView imageView = (ImageView) view.findViewById(R.id.clientPic);
            imageView.setImageResource(R.mipmap.logo);
            Log.d("Loka2", String.valueOf(MapsActivity.iconFinalFinal2));


        return view;
    }

}

Log.d 输出如下所示:

*12-19 21:36:14.499 25315-25315/com.example.yannick.mapdemo D/Lokale 位图:android.graphics.Bitmap@9bb0b83

12-19 21:36:14.526 25315-25315/com.example.yannick.mapdemo D/Loka2: android.graphics.Bitmap@9bb0b83

12-19 21:36:14.672 25315-25315/com.example.yannick.mapdemo D/Lokale 位图:android.graphics.Bitmap@40daa30

12-19 21:36:14.682 25315-25315/com.example.yannick.mapdemo D/Loka2: android.graphics.Bitmap@40daa30

12-19 21:36:14.844 25315-25315/com.example.yannick.mapdemo D/Lokale 位图:android.graphics.Bitmap@4fa0090

12-19 21:36:14.854 25315-25315/com.example.yannick.mapdemo D/Loka2: android.graphics.Bitmap@4fa0090

12-19 21:36:14.948 25315-25315/com.example.yannick.mapdemo D/Loka2: android.graphics.Bitmap@4fa0090

12-19 21:36:15.014 25315-25315/com.example.yannick.mapdemo D/Loka2: android.graphics.Bitmap@4fa0090

12-19 21:36:15.062 25315-25315/com.example.yannick.mapdemo D/Loka2: android.graphics.Bitmap@4fa0090*

每个现有的信息窗口都取最后一个,我不知道为什么....

您只需将每个 Marker 映射到一个图像 ID,并使用此映射来确定要在 getInfoContents() 中使用的图像资源。

首先,将地图定义为实例变量:

Map<Marker, Integer> mMarkerMap = new HashMap<>();

然后每当向地图添加标记时填充此地图:

public void addMarker(LatLng latLng, String title, String snippet, int imageID) {

    MarkerOptions markerOptions = new MarkerOptions()
        .position(latLng)
        .title(title)
        .snippet(snippet);
    Marker marker = mGoogleMap.addMarker(markerOptions);

    mMarkerMap.put(marker, imageID);
}

你可以这样调用上面的方法:

LatLng latLng = new LatLng(37.7244502,-122.4703867);
String title = "title";
String snippet = "snippet";
addMarker(latLng, title, snippet, R.mipmap.logo);

然后修改您的InfoWindowAdapter,以便为特定Marker的InfoWindow使用相应的资源:

public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    private Activity context;

    public CustomInfoWindowAdapter(Activity context){
        this.context = context;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        boolean imageGeandert = false;
        View view = context.getLayoutInflater().inflate(R.layout.custom_infowindow, null);

        TextView tvTitle = (TextView) view.findViewById(R.id.nameTxt);
        TextView tvSubTitle = (TextView) view.findViewById(R.id.addressTxt);

        tvTitle.setText(marker.getTitle());
        tvSubTitle.setText(marker.getSnippet());

        ImageView imageView = (ImageView) view.findViewById(R.id.clientPic);

        //get resource ID and set as image resource:
        int resID =  mMarkerMap.get(marker);
        imageView.setImageResource(resID);

        return view;
    }

}