如何将动态数据从ArrayList设置到InfoWindow中



我很难用动态内容填充InfoWindows。我有一个包含数据的ArrayList,我想填充infoWindow。如果我在标记上使用方法片段,则会动态地显示数据,但全部显示在一行上,因此我不得不使用CustomInfoWindowAdapter。如果我使用CustomInfoWindowAdapter,我会为每个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创建Marker和InfoWindow的方法。

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);
    }

提前感谢您的帮助

我不知道如何修复您的方法,但这就是我实现我的方法的方式。实际上我有两种方法。

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

第二种方法:保存每个标记中的所有动态数据。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();
        }
    }
}

这就是这个例子的layout/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>

最新更新