标记CustomInfoWindow Imageview会覆盖所有其他图像

问题描述 投票:0回答:1

我正在尝试用一些标记来做地图,这些标记应该有一个带图像和文本视图的infolabel。我已经解决了每个infolabel的文本不同但我正在努力与imageview。

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

这是我的codesnippet,我在其中设置了image-和textview的值:

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*

最后一个是为每个现有的infowindow采取的,我不知道为什么....

android google-maps google-maps-markers google-maps-android-api-2 infowindow
1个回答
0
投票

您只需将每个标记映射到图像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,以便为特定标记的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;
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.