【问题标题】:Marker CustomInfoWindow Imageview overrides all other images标记 CustomInfoWindow Imageview 覆盖所有其他图像
【发布时间】:2018-06-02 08:25:08
【问题描述】:

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

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

这是我的代码n-p,我在其中设置了图像和文本视图的值:

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() 中使用什么图像资源。你可以使用HashMap<Marker, Integer>
  • 感谢您的回答,您能给我一个简短的例子吗?

标签: android google-maps google-maps-markers google-maps-android-api-2 infowindow


【解决方案1】:

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

}

【讨论】:

  • 感谢您的帮助,但在 CustomWindowInfoAdapter 中我得到:“无法解析为符号 mMarkerMap”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 2020-08-10
  • 1970-01-01
相关资源
最近更新 更多