Java 如何为不同的标记创建不同的信息窗口?

Java 如何为不同的标记创建不同的信息窗口?,java,android,google-maps,Java,Android,Google Maps,我目前正在使用谷歌地图API v2为Android开发一个应用程序。即使我在大学里学习过Java(我现在还在学习),我对android开发还是很陌生 我成功地创建了地图,并在地图上添加了我的个人标记。每个标记都需要自己的信息窗口,这就是问题所在。我只能修改标题和片段,但在框中,我还需要为不同的标记放置不同的图片 标记表示库 例如: 标记1: 名称 服务(空调、食品、插头等的不同图像) 可用座位数 名字不是问题:它是标题。座位也由片段表示。但是图像是个问题:我找不到在不同的窗口中显示不同图像的方法

我目前正在使用谷歌地图API v2为Android开发一个应用程序。即使我在大学里学习过Java(我现在还在学习),我对android开发还是很陌生

我成功地创建了地图,并在地图上添加了我的个人标记。每个标记都需要自己的信息窗口,这就是问题所在。我只能修改标题和片段,但在框中,我还需要为不同的标记放置不同的图片

标记表示库

例如: 标记1: 名称 服务(空调、食品、插头等的不同图像) 可用座位数

名字不是问题:它是标题。座位也由片段表示。但是图像是个问题:我找不到在不同的窗口中显示不同图像的方法

以下是主要活动的Java代码:

package com.matteocunsolo.apposto;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
导入com.google.android.gms.maps.MapFragment;
导入com.google.android.gms.maps.model.CameraPosition;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.Marker;
导入com.google.android.gms.maps.model.MarkerOptions;
导入android.os.Bundle;
导入android.support.v4.app.FragmentActivity;
导入android.view.view;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MainActivity扩展了FragmentActivity{
类MyInfoWindowAdapter实现InfoWindowAdapter{
私有最终视图myContentsView;
MyInfoWindowAdapter(){
myContentsView=getLayoutFlater().充气(R.layout.custom\u info\u contents,null);
}
@凌驾
公共视图getInfoContents(标记器){
TextView tvTitle=((TextView)myContentsView.findViewById(R.id.title));
tvTitle.setText(marker.getTitle());
TextView tvSnippet=((TextView)myContentsView.findViewById(R.id.snippet));
tvsippet.setText(marker.getSnippet());
返回myContentsView;
}
@凌驾
公共视图getInfoWindow(标记器){
//TODO自动生成的方法存根
返回null;
}
}
//谷歌地图
私人谷歌地图谷歌地图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
试一试{
//装载图
初始化映射();
}捕获(例外e){
e、 printStackTrace();
}
}
/**
*函数加载映射。如果未创建映射,它将为您创建映射
* */
私有无效初始值映射(){
if(googleMap==null){
googleMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.getUiSettings().SetZoomGetUreSEnabled(true);
if(谷歌地图!=null){
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
}
googleMap.getUiSettings().setRotategestureEnabled(true);
CameraPosition CameraPosition=new CameraPosition.Builder().target(new LatLng(41.903021,12.514744)).zoom(15.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
createMarkers();
setInfoWindowAdapter(新的MyInfoWindowAdapter());
//检查是否成功创建映射
if(googleMap==null){
Toast.makeText(getApplicationContext(),
“抱歉!无法创建地图”,Toast.LENGTH\u SHORT)
.show();
}
}
}
void createMarkers(){
String name=“Biblioteca Alessandrina”;
双纬度=41.903021;
双经度=12.514744;
Marker m=googleMap.addMarker(新MarkerOptions().title(name).position(新LatLng(纬度,经度)).snippet(“250”);
字符串名称2=“Biblioteca CIAO”;
双纬度2=41.906332;
双纵2=12.517669;
Marker m2=googleMap.addMarker(新MarkerOptions().title(name2+“\n”).position(新LatLng(latitude2,longitude2)).snippet(“134”);
}
@凌驾
受保护的void onResume(){
super.onResume();
初始化映射();
}
}
下面是信息窗口的XML:

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


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

       <TextView
           android:id="@+id/title"
           android:layout_width="wrap_content"
           android:layout_height="30dp"
           android:layout_marginLeft="5dp"
           android:text="title"
           android:textSize="25dp"
           android:textStyle="bold" />

       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:orientation="horizontal" >

           <TextView
               android:layout_width="wrap_content"
               android:layout_height="23dp"
               android:layout_marginLeft="5dp"
               android:text="Services:   "
               android:textSize="20dp" />

           <ImageView
               android:layout_width="20dp"
               android:layout_height="20dp"
               android:layout_marginRight="5dp"
               android:layout_marginTop="3dp"
               android:adjustViewBounds="true"
               android:src="@drawable/ic_launcher" />

           <ImageView
               android:layout_width="20dp"
               android:layout_height="20dp"
               android:layout_marginRight="5dp"
               android:layout_marginTop="3dp"
               android:adjustViewBounds="true"
               android:src="@drawable/ic_launcher" />

           <ImageView
               android:layout_width="20dp"
               android:layout_height="20dp"
               android:layout_marginRight="5dp"
               android:layout_marginTop="3dp"
               android:adjustViewBounds="true"
               android:src="@drawable/ic_launcher" />
       </LinearLayout>

       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:orientation="horizontal" >

           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginLeft="5dp"
               android:text="Seats:   "
               android:textSize="20dp" />
           <TextView
               android:id="@+id/snippet"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginLeft="5dp"
               android:textSize="20dp"
               android:text="150" />
       </LinearLayout>
    </LinearLayout>
</LinearLayout>

我只能修改标题和片段,但在框中,我还需要为不同的标记放置不同的图片

您的
getInfoContents()
方法肯定会有更大的变化

但是图像是个问题:我找不到在不同的窗口中显示不同图像的方法


使用
findViewById()
检索
ImageView
,并在
getInfoContents()
方法中设置要用于它的图像。这与您更改
文本视图
小部件的方式相同。

非常感谢。我试过这种方法,实际上我解决了这个问题。但部分原因是。事实上,我试过用两个标记,其中一个显示2个图像,另一个显示3个。它起作用了,但如果我点击带有2张图片的标记,我会找到正确的2张。位如果我先单击带有3张图片的标记,然后单击带有两张图片的标记,我仍然可以看到另一个标记的3张图片。