如何在Google Maps Android中使用不同的信息窗口创建2个标记?

时间:2014-06-25 23:24:58

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

我只是想知道我可以使用不同的信息窗口创建多个标记,在Android中的谷歌地图v2中,在我的情况下,我尝试3种不同的标记, 1.学校标记 2.方向(fromPosition) 3.方向(toPosition)

注意:nama_sma>>学校名称      :nama_bemo>>出租车

现在,我可以显示所有这些标记,但每个标记的问题都有相同的信息窗口, 这是我的xml:

   
 <com.example.frontend.search.MapWrapperLayout      
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

 <fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/map"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/layout1"
    class="com.google.android.gms.maps.SupportMapFragment"/>
    </com.example.frontend.search.MapWrapperLayout>

nb:MapWrapperLayout是我的应用程序中自定义信息窗口的包装器

这是我的活动

public class Map_result extends FragmentActivity
{   

    GoogleMap map;
    private DBDataSource dataSource;
    private ViewGroup infoWindow;
    private TextView infoTitle;
    private TextView infoSnippet;
    private Button infoButton2;
    //this class for adding button in info window
    private OnInfoWindowElemTouchListener infoButtonListener2;


    LatLng queryPoint;


    LatLng queryPoint_jalur, queryPoint_jalur2; 
  //nama_bemo == taxi
String nama_bemo="";
String latitude_asal="";
String longitude_asal="";
String latitude_tujuan="";
String longitude_tujuan="";
String temp_jalur="";
String temp_jalur2="";
  Double lat_jalur=0.0; 
  Double lon_jalur=0.0;
  Double lat_jalur2=0.0; 
  Double lon_jalur2=0.0;

     String lat, lon;
 String temp, nama_sma;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);
            dataSource = = new DBDataSource(this);
            //make the database open
       dataSource.open();

            map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        this.infoWindow     = (ViewGroup)getLayoutInflater().inflate(R.layout.custom_info_window, null);
        this.infoTitle      = (TextView)infoWindow.findViewById(R.id.title);
        this.infoSnippet    = (TextView)infoWindow.findViewById(R.id.snippet);
        this.infoButton2    = (Button)infoWindow.findViewById(R.id.button2);
            this.infoButtonListener2 = new OnInfoWindowElemTouchListener(infoButton2,
                getResources().getDrawable(R.drawable.custom_btn_beige),
                getResources().getDrawable(R.drawable.custom_btn_orange)) {

            @Override
            protected void onClickConfirmed(View v, Marker marker) {
                // TODO Auto-generated method stub

                }
                            }
        };
        this.infoButton2.setOnTouchListener(infoButtonListener2);
            //method to see the map
            seeMap();

       private void seeMap()
       {
         final MapWrapperLayout mapWrapperLayout = (MapWrapperLayout)findViewById(R.id.map_relative_layout);

           //an array list for getting 2 point (fromPosition) and (toPosition) for taxi
    ArrayList<Sma> list_jalur = dataSource.getallPoint_Jalur_SMA(id_sma);

        for  (int i2 = 0; i2 < list_jalur.size(); i2++) 
        {
           nama_bemo        = list_jalur.get(i2).getNama_bemo();

           latitude_asal    = list_jalur.get(i2).getLat_asal_bemo();
           longitude_asal   = list_jalur.get(i2).getLon_asal_bemo();


           latitude_tujuan  = list_jalur.get(i2).getLat_tujuan_bemo();
           longitude_tujuan     = list_jalur.get(i2).getLon_tujuan_bemo();

           temp_jalur       = latitude_asal + longitude_asal;

           temp_jalur2      = latitude_tujuan + longitude_tujuan;

            String[] splitCoordinate_jalur = temp_jalur.split(", ");
            lat_jalur = Double.valueOf(splitCoordinate_jalur[0]); 
            lon_jalur = Double.valueOf(splitCoordinate_jalur[1]); 

                    //this LatLng (from Position)
                    queryPoint_jalur = new LatLng(lat_jalur, lon_jalur);

                    String[] splitCoordinate_jalur2 = temp_jalur2.split(", ");
            lat_jalur2 = Double.valueOf(splitCoordinate_jalur2[0]);
            lon_jalur2 = Double.valueOf(splitCoordinate_jalur2[1]);

                    //this LatLng (from toPosition) 
            queryPoint_jalur2 = new LatLng(lat_jalur2, lon_jalur2);
               //if the type of taxi is A
               if(nama_bemo.equals("A"))
               {

                 map.addPolyline(new PolylineOptions()
                .add(queryPoint_jalur, queryPoint_jalur2)
                .width(7)
                .color(Color.RED));

                 // the question is right here, until here, 
                 //i can create the marker in my map, but all the marker is 
                 //have 1 same info windows. in my case, in this code below, 
                 //I try to make a marker with different infoWindow
                 map.addMarker(new MarkerOptions()
                    .position(queryPoint_jalur2)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_smk)));

                    map.addMarker(new MarkerOptions()
                    .position(queryPoint_jalur)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_smk)));

                     }

               }
       //this loop is getting all school data
      for (int i = 0; i < this.list.size(); i++) 
      {
             //school name
         nama_sma       = list.get(0).getNama().toString();                 
         lat            = list.get(i).getLatitude().toString();
         lon            = list.get(i).getLongitude().toString();

         temp   = lat + lon;

             String[] splitCoordinate = temp.split(", "); 
         a = Double.valueOf(splitCoordinate[0]);
         b = Double.valueOf(splitCoordinate[1]);

             //this is for get the marker of school position
             queryPoint = new LatLng(a, b);

             map.setMyLocationEnabled(true);
                 map.moveCamera(CameraUpdateFactory.newLatLngZoom(queryPoint,13));
                 //this is is setup the custom info window
                 map.setInfoWindowAdapter(new InfoWindowAdapter() {
                        @Override
                        public View getInfoWindow(Marker marker) {
                            // TODO Auto-generated method stub
                                infoTitle.setText(marker.getTitle());
                            infoSnippet.setText(marker.getSnippet());
                            infoButtonListener2.setMarker(marker);
                            mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
                            return infoWindow;
                        }

                        @Override
                        public View getInfoContents(Marker marker) {
                            // TODO Auto-generated method stub
                            return null;
                        }
                    });
                                   map.addMarker(new MarkerOptions()
                        .position(queryPoint)
                        .title(nama_sma)
                  }

问题是,3 Marker(fromPosition),(toPosition)和(School Marker)显示相同的信息窗口,任何人都可以帮助我吗?在每个标记中创建不同的信息窗口?需要任何帮助。谢谢你

1 个答案:

答案 0 :(得分:0)

编辑:如果您希望为每个标记设置不同的信息窗口,则还必须为每个标记创建一个视图。为标记的信息窗口创建一个Map,并使用Marker id作为键,以便检查是否已创建View。

尝试这样的事情:

private Map<String,ViewGroup> infoWindows = new HashMap<String,ViewGroup>();

...

 @Override
 public View getInfoWindow(Marker marker) {
    // get or create the infoWindows here instead of in onCreate
    ViewGroup infoWindow = infoWindows.get(marker.getId());
    if(infoWindow==null){
        //if the info window was not yet created for this marker, create it
        infoWindow = (ViewGroup)getLayoutInflater().inflate(R.layout.custom_info_window, null);
        Button infoButton = (Button)infoWindow.findViewById(R.id.button2);
        infoButton.setOnTouchListener(infoButtonListener2);
        infoWindows.put(marker.getId(), infoWindow);
    }

    // set or update the texts
    ((TextView)infoWindow.findViewById(R.id.title)).setText(marker.getTitle());
    ((TextView)infoWindow.findViewById(R.id.snippet)).setText(marker.getSnippet());

    //why do you need to set the marker to the listener?
    infoButtonListener2.setMarker(marker);
    mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);

    return infoWindow;
 }