如何在不同的信息窗口上显示不同的图像

时间:2014-06-01 01:42:56

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

基本上问题是当我打开任何标记时,出现的infowindow总是有相同的图像,但标题和片段没问题。

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

这是我的代码: 的 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>

和另一个问题:

正如我对每个标记的每个infowindow所做的那样,点击时打开一个不同的片段。

感谢。

1 个答案:

答案 0 :(得分:1)

你所做的是你只需要将一个图像放到infoWindow,如果你想拥有不同的图像,你可以将标记和图像资源放在HashMap结构中,Marker作为key和image resource作为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)它将检查标记是否已经在HashMap中。

(getResources().getDrawable(yourhashMap.get(marker)));它将获取作为图像资源的HashMap的值

<强>样品:

创建一个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);

<强> editted:

    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();        
}