2014-10-16 59 views
2

我在使用动态内容填充InfoWindows时遇到困难。我有一个ArrayList与数据,我想填充infoWindow。如果我在标记上使用方法代码片段,请动态显示数据,但全部在一行上,所以我不得不使用CustomInfoWindowAdapter。如果我使用CustomInfoWindowAdapter,则为每个InfoWindow显示相同的数据。我错了什么?如何将来自ArrayList的动态数据设置为InfoWindow

这是我CustomInfoWindowAdapter:

class MarkerInfoWindowAdapter implements InfoWindowAdapter { 

     private View inflatedView; 
     private View tempView; 
     private String title, description; 

     public void setTitle(String title) { 
      this.title = title; 
     } 

     public void setDescription(String description) { 
      this.description = description; 
     } 

     MarkerInfoWindowAdapter() { 
      inflatedView = getLayoutInflater().inflate(R.layout.info_window_layout, null); 
     // tempView = getLayoutInflater().inflate(R.layout.location_content_window, 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) { 
      final TextView txtTitle = (TextView) view.findViewById(R.id.title); 
      final TextView txtDescription = (TextView) view.findViewById(R.id.lat_lng); 
      txtTitle.setText(title); 
      txtDescription.setText(description); 
     } 
    } 

而这正是我创建了标记,并从ArrayList中的信息窗口的方法。

public void drawRoute(HashMap<String, ArrayList<HashMap<String, String>>> result, String type) { 
     ArrayList<HashMap<String, String>> voyageplan = result.get("optimal"); 
     HashMap<String, String> waypoint1; 

     for (int i = 0; i < voyageplan.size(); i++) { 

      waypoint1 = voyageplan.get(i);   

      googleMap.addMarker(new MarkerOptions().position(position) 
      .title("WP# "+waypoint1.get("WP").toString()+" (optimal)") 
      .snippet("prova") 
      .anchor(0.5f, 0.5f).draggable(false)  
      .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA))); 

      infoWindowAdapter = new MarkerInfoWindowAdapter(); 
      infoWindowAdapter.setTitle(voyageplan.get(i).get("WP")); 
      infoWindowAdapter.setDescription(voyageplan.get(i).get("lat")); 
      googleMap.setInfoWindowAdapter(infoWindowAdapter); 
    } 

预先感谢您的帮助

回答

0

我不知道如何解决你的方法,但是这是我如何实现我的这工作。实际上我有两种方法。

第一种方法:创建另一个函数来为您获取数据 并在您的mapInitialization中调用该函数。

第二种方法:保存所有动态数据在每个marker.snippet 属性为逗号分隔值,并且通过处理分离的那些逗号分隔存储的数据 每个标记提取您信息 。

我实现了下面的第一个方法。

// This function is where you read from your array, web, REST API or 
// whatever to get the dynamic data needed to put into your markers. 
//I just used a simple case of if and else to show dynamically changing 
//string is returned. 

private String getDescriptionFromMarkerSoon(double latitude, double longitude) { 
    // Access database or data structure here using arguments as key 
    if (longitude > 0) { 
     return "I am a boy"; 
    } else { 
     return "I am a girl"; 
    } 
} 

// This is the one time setup that you need to do inside the setUpMapIfNeeded() 
// function that is created by Android Studio if you initialize your project 
// as a Google Maps Template. 
private void setUpMapIfNeeded() { 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (mMap == null) { 
     // Try to obtain the referecen of the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     // Check if we were successful in obtaining the map. 
     if (mMap != null) { 
      // add the layout for customizedInfoWindow in Google Maps 
      mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 

       @Override 
       // Returning null here will automatically call getInfoContents 
       public View getInfoWindow(Marker arg0) { 
        return null; 
       } 

       // Get the information to be displayed on the current window 
       @Override 
       public View getInfoContents(Marker marker) { 
        // Get the layout file for InfoContent 
        View v = getLayoutInflater().inflate(R.layout.info_window, null); // null means dont attach to any parent window 
        TextView tvDescription = (TextView) v.findViewById(R.id.tv_description); 
        TextView tvSnippet = (TextView) v.findViewById(R.id.tv_snippet); 
        TextView tvLat = (TextView) v.findViewById(R.id.tv_lat); 
        TextView tvLng = (TextView) v.findViewById(R.id.tv_lng); 

        // Get position of marker 
        LatLng markerPos = marker.getPosition(); 
        tvLat.setText("Latitude: " + markerPos.latitude); 
        tvLng.setText("Longitude: " + markerPos.longitude); 
        tvSnippet.setText(marker.getSnippet()); 
        // Call the function you created here to get 
        // the dynamic data 
        tvDescription.setText(getDescriptionFromMarkerSoon 
        (markerPos.latitude, markerPos.longitude)); 
        // Return the view created 
        return v; 
       } 
      }); 
      setUpMap(); 

     } 
    } 
} 

这是我的布局/ info_window.xml文件看起来像这个例子

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

    <TextView 
     android:id="@+id/tv_description" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="15sp" 
     android:textStyle="bold" 
     /> 


    <TextView android:id="@+id/tv_snippet" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="10sp" 
     /> 

    <TextView 
     android:id="@+id/tv_lat" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="5sp" 

     /> 

    <TextView android:id="@+id/tv_lng" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="5sp" 

     /> 

    </LinearLayout> 
</LinearLayout> 
相关问题