2014-10-01 73 views
1
public void marker1(final List<String> listName, 
     final List<String> listIllness, final List<String> listIC, 
     final List<String> listElderlyimage, List<String> listArduinomac, 
     List<String> listLat, List<String> listLong) { 


    Log.e("", Integer.toString(listName.size())); 
    for (int i = 0; i < listName.size(); i++) { 
     name = listName.get(i); 
     ic = listIC.get(i); 
     illness = listIllness.get(i); 
     Double Lat = Double.parseDouble(listLat.get(i)); 
     Double Long = Double.parseDouble(listLong.get(i)); 
     MarkerOptions marker = new MarkerOptions().position(new LatLng(Lat, 
       Long)); 

     marker.icon(BitmapDescriptorFactory 
       .defaultMarker(BitmapDescriptorFactory.HUE_VIOLET)); 


     infoWindowAdapter = new MarkerInfoWindowAdapter(); 
     googleMap.setInfoWindowAdapter(infoWindowAdapter); 
     infoWindowAdapter.setName(name); 
     infoWindowAdapter.setIC(ic); 
     infoWindowAdapter.setIllness(illness); 
     googleMap.addMarker(marker); 


    }// for loop 

}// marker1 

列表的最後一個元素。這是我使用自定義信息窗口只顯示在android系統

class MarkerInfoWindowAdapter implements InfoWindowAdapter { 

    private View inflatedView; 
    private String name, ic, illness; 

    public void setName(String name) { 
     this.name = name; 
    } 

    public void setIC(String ic) { 
     this.ic = ic; 
    } 

    public void setIllness(String illness) { 
     this.illness = illness; 
    } 

    MarkerInfoWindowAdapter() { 
     inflatedView = getActivity().getLayoutInflater().inflate(
       R.layout.custom_info_contents, null); 

    } 

    @Override 
    public View getInfoContents(Marker marker) { 
     setInfo(marker, inflatedView); 
     return inflatedView; 
    } 

    @Override 
    public View getInfoWindow(Marker marker) { 
     setInfo(marker, inflatedView); 
     return inflatedView; 
    } 

    private void setInfo(Marker marker, View view) { 
     TextView txtname = (TextView) view 
       .findViewById(R.id.txtPatientName); 
     TextView txtIC = (TextView) view.findViewById(R.id.txtIC); 
     TextView txtIllness = (TextView) view.findViewById(R.id.txtIllness); 
     txtname.setText(name); 
     txtIC.setText(ic); 
     txtIllness.setText(illness); 
     Log.e("<>", txtname.toString()); 
    } 
} 

您好定製infowindowadapter,我不知道爲什麼,每當我點擊該標記,它不斷顯示同一人的信息。我不確定哪裏出了問題。請幫幫我!謝謝。

回答

1

它不斷顯示同一個人信息

那是因爲你只有在一個MarkerInfoWindowAdapter「人員信息」,而你忽略傳遞到getInfoWindow()Marker。如果要根據Marker填充信息窗口,則需要查找與Marker相關的信息。 This sample application顯示根據Marker ID查找模型數據。

此外,請勿同時執行getInfoWindow()getInfoContents()。如果它返回非null值,則僅使用一個 - getInfoWindow(),否則返回getInfoContents()。在這種情況下,由於您始終返回getInfoWindow()的非null值,因此將永遠不會調用getInfoContents()

此外,請勿爲每個Marker創建InfoWindowAdapter。只有一個InfoWindowAdapter被使用。在你的情況下,這將是你創建的最後一個。

+0

所以我可以有一個標記列表?並將它們傳遞到自定義信息窗口中? – 2014-10-02 02:43:24

+0

@MarcusKoh:'InfoWindowAdapter'需要一種方法來根據提供給'getInfoWindow()'或'getInfoContents()'的'Marker'來查找要顯示的細節。你如何實現這一切取決於你。 – CommonsWare 2014-10-02 11:20:29