【问题标题】:Inserting map marker icon in marker infowindow在标记信息窗口中插入地图标记图标
【发布时间】:2017-11-12 21:23:20
【问题描述】:

我正在开发一个 Android 应用程序。 我需要将地图标记图标传递给它的信息窗口。

这是addMarker方法:

  private void addMarker(LatLng latlng, final String title, final String nombre_apellidos, final String datosreporte,final  String tiporeporte) {
        markerOptions.position(latlng);
        markerOptions.title(title);
        markerOptions.snippet(nombre_apellidos+": "+datosreporte);
        if (tiporeporte.equals("1")){
            markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.tipo_1));
        }
        else{
            markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_actihome));
        }


        mGoogleMap.addMarker(markerOptions).showInfoWindow();

        mGoogleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                Toast.makeText(getContext(), marker.getTitle(), Toast.LENGTH_SHORT).show();
            }
        });
    }

这就是我创建信息窗口的方式:

 public View getInfoWindow(Marker arg0) {
                return null;
            }

            // Defines the contents of the InfoWindow
            @Override
            public View getInfoContents(Marker arg0) {
                View v = null;
                try {

                    // Getting view from the layout file info_window_layout
                    v = getLayoutInflater().inflate(R.layout.custominfowindow, null);

                    // Getting reference to the TextView to set latitude
                    TextView addressTxt = (TextView) v.findViewById(R.id.addressTxt);
                    TextView nameTxt = (TextView) v.findViewById(R.id.nameTxt);
                    ImageView icono = (ImageView) v.findViewById(R.id.iconoimagen);
                    addressTxt.setText(arg0.getTitle());
                    nameTxt.setText(arg0.getSnippet());



                } catch (Exception ev) {
                    System.out.print(ev.getMessage());
                }

                return v;
            }

我需要将标记图标放在 ImageView 图标中。

谢谢。

【问题讨论】:

  • 不是最好的解决方案,但你可以试试这个链接stackoverflow.com/a/23237766/1548824
  • 另一种解决方案是您可以使用 markeroption.setTag(yourvalue) 并在 getInfoWindow agr0.getTag(); 中获取值,这将转换您自定义信息窗口中的值
  • @akhilesh0707, 选项 setTag 不适用于 markeroption....
  • @mvasco 如果您想获得图标,您有两个选择。首先将您的标记保存在哈希映射或其他东西中,然后从那里获取第二个是使用 markeroption.getIcon() 来获取图标。但我认为 getIcon 方法仅在谷歌地图的 web api 中可用。

标签: android google-maps


【解决方案1】:

传递给自定义 InfoWindowAdapter 方法的唯一对象是 Marker。

您可以通过在 Marker 对象上设置标签来将信息从活动传递到您的自定义 InfoWindowAdapter。您可以通过调用 setTag 方法传递一个对象来将标签对象添加到 Marker。并从 InfoWindowAdapter 中的标记中获取对象并使用这些值。

您可以创建数据传输对象,其中包含您想在地图上的信息窗口中显示的所有元素。

public class InfoWindowData {
    private String image;
    private String hotel;

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getHotel() {
        return hotel;
    }

    public void setHotel(String hotel) {
        this.hotel = hotel;
    }
}

在你的活动中

InfoWindowData data = new InfoWindowData();
data.setImage("blah");
data.setHotel("blah blah");

marker.setTag(data);

在自定义信息窗口类中

@Override
    public View getInfoContents(Marker marker) {

        InfoWindowData data = (InfoWindowData) marker.getTag();
    ....

完整示例http://www.zoftino.com/google-maps-android-custom-info-window-example

【讨论】:

    【解决方案2】:

    更新您的 Google Maps Android API v2 版本 9.4.0

    com.google.android.gms:play-services:9.4.0
    

    而不是这个

    mGoogleMap.addMarker(markerOptions).showInfoWindow();
    

    试试这个,它会返回一个标记对象,然后你可以自定义值setTag()

    Marker marker=mGoogleMap.addmarker(new MarkerOptions(LatLang));
    // Set your object value as tag
    marker.setTag(yourValue)
    

    // 从标记中获取值

    arg0.getTag();// Type cast to your object type;
    

    更多信息请参考link

    【讨论】:

      猜你喜欢
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 2011-02-24
      • 2014-09-26
      相关资源
      最近更新 更多