【问题标题】:how to display different images on different infowindow如何在不同的信息窗口上显示不同的图像
【发布时间】:2014-06-01 01:42:56
【问题描述】:

基本上问题是当我打开任何标记时,出现的信息窗口总是有相同的图像,但标题和 sn-p 没有问题。

我不知道每个标记特定于不同的图像,但不知道如何。 (图片在文件夹抽屉里)

这是我的代码: Fragmentavtivity.java

public class Fragment17 extends SherlockFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment17, container, false);
    return rootView;
}

@Override
public void onViewCreated(View v, Bundle savedInstanceState){
    super.onViewCreated(v, savedInstanceState);

final LatLng Initial = new LatLng(-34.673009, -58.474111);

final LatLng FADU = new LatLng(-34.542163, -58.443716);

final LatLng UNO = new LatLng(-34.524924, -58.576421);
final LatLng DOS = new LatLng(-34.755415, -58.577794);


GoogleMap googlemap;

    googlemap  = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map17)).getMap();

    googlemap.setMyLocationEnabled(true);
    googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(Initial, 10);
    googlemap.animateCamera(update);

    googlemap.addMarker(new MarkerOptions().position(FADU).title("FADU").snippet("Facultad de Arquitectura, Diseño y Urbanismo").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));

    googlemap.addMarker(new MarkerOptions().position(UNO).title("TEOREMA").snippet("san matin 1245"));
    googlemap.addMarker(new MarkerOptions().position(DOS).title("El Mundo del Acrilico").snippet("san benito 2144/"));


    googlemap.setInfoWindowAdapter(new InfoWindowAdapter() {

     @Override
     public View getInfoContents(Marker marker) {

         View v = getLayoutInflater(null).inflate(R.layout.infowindow, null);

         TextView titulo = (TextView) v.findViewById(R.id.titulo);
         TextView direccion = (TextView) v.findViewById(R.id.direccion);
         ImageView imagen = ((ImageView)v.findViewById(R.id.imagen));

         titulo.setText(marker.getTitle());
         direccion.setText(marker.getSnippet());
         imagen.setImageDrawable(getResources().getDrawable(R.drawable.teorema));

         return v;

     }

    @Override
    public View getInfoWindow(Marker marker) {
        // TODO Auto-generated method stub
        return null;
    }
 });
}


@Override
public void onPause() {
    super.onPause();

 }
@Override
public void onDestroyView() {

    super.onDestroyView(); 
    Fragment fragment = (getFragmentManager().findFragmentById(R.id.map17));  
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();        
}

infowindow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView 
    android:id="@+id/imagen"
    android:layout_width="65dp"
    android:layout_height="99dp"
    android:contentDescription="@string/app_name"/>


<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/titulo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center_horizontal|center"
    android:textSize="15sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/direccion"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center_horizontal|center"
    android:textSize="12sp" />

</LinearLayout>
</LinearLayout>

还有一个问题:

就像我对每个标记的每个信息窗口所做的那样,单击时打开一个不同的片段。

谢谢。

【问题讨论】:

  • 您无需将标题更改为 (SOLVED)。 SO 将自行处理(黄色数字)。

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


【解决方案1】:

您所做的只是将单个图像放入 infoWindow,如果您想要不同的图像,您可以将 Marker 和 Image 资源放在 HashMap 结构中,Marker 作为键,@ 987654323@作为HashMap的值。

示例:

@Override
 public View getInfoContents(Marker marker) {

     View v = getLayoutInflater(null).inflate(R.layout.infowindow, null);

     TextView titulo = (TextView) v.findViewById(R.id.titulo);
     TextView direccion = (TextView) v.findViewById(R.id.direccion);
     ImageView imagen = ((ImageView)v.findViewById(R.id.imagen));

     titulo.setText(marker.getTitle());
     direccion.setText(marker.getSnippet());

     if(yourhashMap.get(marker) != null)
        imagen.setImageDrawable(getResources().getDrawable(yourhashMap.get(marker)));

     return v;

 }

if(yourhashMap.get(marker) != null) 会检查 Marker 是否已经在 HashMap 中。

(getResources().getDrawable(yourhashMap.get(marker)));会得到图片资源HashMap的值

样本:

创建一个哈希图:

private HashMap<Marker, Integer> hash = new HashMap<Marker, Integer>();

每次向地图添加标记时,将其添加到哈希映射中

    Marker marker = new MarkerOptions().position(FADU).title("FADU").snippet("Facultad de Arquitectura, Diseño y Urbanismo").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
    googlemap.addMarker(marker);
    hash.put(marker, R.drawable.the drawable if the marker);

已编辑:

    public class Fragment17 extends SherlockFragment {

    private HashMap<Marker, Integer> hash = new HashMap<Marker, Integer>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment17, container, false);
    return rootView;
}

@Override
public void onViewCreated(View v, Bundle savedInstanceState){
    super.onViewCreated(v, savedInstanceState);

final LatLng Initial = new LatLng(-34.673009, -58.474111);

final LatLng FADU = new LatLng(-34.542163, -58.443716);

final LatLng UNO = new LatLng(-34.524924, -58.576421);
final LatLng DOS = new LatLng(-34.755415, -58.577794);


GoogleMap googlemap;

    googlemap  = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map17)).getMap();

    googlemap.setMyLocationEnabled(true);
    googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(Initial, 10);
    googlemap.animateCamera(update);

    Marker marker1 = googlemap.addMarker(new MarkerOptions().position(FADU).title("FADU").snippet("Facultad de Arquitectura, Diseño y Urbanismo").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
    hash.put(marker1, R.drawable.the drawable if the marker);

    Marker marker2 = googlemap.addMarker(new MarkerOptions().position(UNO).title("TEOREMA").snippet("san matin 1245"));
    hash.put(marker2, R.drawable.the drawable if the marker);

    Marker marker3 = googlemap.addMarker(new MarkerOptions().position(DOS).title("El Mundo del Acrilico").snippet("san benito 2144/"));
    hash.put(marker3, R.drawable.the drawable if the marker);

    googlemap.setInfoWindowAdapter(new InfoWindowAdapter() {

     @Override
     public View getInfoContents(Marker marker) {

     View v = getLayoutInflater(null).inflate(R.layout.infowindow, null);

     TextView titulo = (TextView) v.findViewById(R.id.titulo);
     TextView direccion = (TextView) v.findViewById(R.id.direccion);
     ImageView imagen = ((ImageView)v.findViewById(R.id.imagen));

     titulo.setText(marker.getTitle());
     direccion.setText(marker.getSnippet());

     if(yourhashMap.get(marker) != null)
        imagen.setImageDrawable(getResources().getDrawable(yourhashMap.get(marker)));

     return v;

 }

    @Override
    public View getInfoWindow(Marker marker) {
        // TODO Auto-generated method stub
        return null;
    }
 });
}


@Override
public void onPause() {
    super.onPause();

 }
@Override
public void onDestroyView() {

    super.onDestroyView(); 
    Fragment fragment = (getFragmentManager().findFragmentById(R.id.map17));  
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();        
}

【讨论】:

  • 但是在这个例子中,我在什么时候指定图像?例如,我有 Image1.png 作为marker1 的信息窗口,image2.png 作为marker2 的信息窗口
  • @user3571227 将标记添加到地图时,然后将标记添加到哈希图
  • 还有一个 hashmap 的例子?我从来没有真正使用过
  • 我无法用哈希图解决这个问题,如果你给我一个具体的例子,我的特殊情况会更好理解。抱歉迟到了
  • 谢谢你们,现在我知道我的错误了
猜你喜欢
  • 2013-06-08
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
  • 2022-10-23
  • 2019-01-10
  • 2021-07-07
  • 1970-01-01
相关资源
最近更新 更多