2014-06-25 36 views
1

我只是不知道我可以用不同的信息窗口創建多個標記,在谷歌地圖V2 Android中,在我的情況下,在這裏,我嘗試3個不同的標記, 1.學校標記 2.方向(fromPosition) 3。方向(toPosition)如何在Google Maps Android中使用不同的信息窗口創建2個標記?

注: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> 

注: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個標記(fromPosition),(toPosition),和(學校標記)顯示相同的信息窗口,任何人都可以幫助我嗎?在每個標記中創建不同的信息窗口?任何幫助是必要的。先謝謝

+0

請澄清你所說的「顯示相同的信息窗口」的意思,我可能沒有得到這一權利。 – tknell

+0

請參閱http://stackoverflow.com/questions/23143969/multiple-infowindowadatpers – user2808624

回答

0

編輯:如果你想爲每個標記有一個不同的信息窗口,你還必須爲它們中的每一個創建一個視圖。爲標記的信息窗口創建一個地圖並使用標記ID作爲關鍵字,以便您可以檢查是否已經創建了視圖。

嘗試用這樣的事情:

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; 
} 
+0

是的,我已經這樣做了,但是我想創建不同的信息窗口,而不是不同的文本。 – user3747208

+0

你是什麼意思與「不同的信息窗口」 ?你想一次顯示所有三個信息窗口嗎?或者你想使用自定義視圖的信息窗口? – tknell

+0

在我的情況下,我已經一次顯示3個標記,但每個標記都顯示相同的信息窗口,所以我試圖在每個標記上製作不同的信息窗口 – user3747208

相關問題