【问题标题】:How set dynamic data from ArrayList into InfoWindow如何将动态数据从 ArrayList 设置到 InfoWindow
【发布时间】:2014-12-12 02:17:40
【问题描述】:

我在使用动态内容填充 InfoWindows 时遇到了困难。我有一个 ArrayList,其中包含我想填充 infoWindow 的数据。如果我在标记上使用方法 sn-p,动态显示数据,但全部显示在一行上,所以我不得不使用 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);
    }

提前感谢您的帮助

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    // 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-09
      • 2017-12-16
      • 2021-01-15
      • 2014-10-18
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      • 2014-04-19
      相关资源
      最近更新 更多