【问题标题】:Customised view of InfoWindow for a list of markers in Google Maps V2 AndroidGoogle Maps V2 Android 中标记列表的 InfoWindow 自定义视图
【发布时间】:2013-08-26 00:45:34
【问题描述】:

我正在尝试创建我已为其制作布局文件的信息窗口的自定义视图。 但我的数据(纬度和经度)是 ArrayList 的形式。

for (int i = 0; i < list.size(); i++) {
            Log.e(getClass().getName(), list.get(i).getFirstName());
            double x = list.get(i).getLocationLatLon().getLat();
            double y = list.get(i).getLocationLatLon().getLon();
            if (x > 90 || x < -90 || y < -180 || y > 180) {
                Log.e(getClass().getName(),
                        "out of range " + Double.toString(x) + "  "
                                + Double.toString(y));
                lng2 = new LatLng(lat, lng);
            } else {
                Log.e(getClass().getName(),
                        "In range " + Double.toString(x) + "  "
                                + Double.toString(y));
                lng2 = new LatLng(x, y);
            }

            InfoClass infoClass= new InfoClass();
            infoClass.setName(list.get(i).getFirstName()+" "+list.get(i).getLastName());
            infoClass.setCharge(list.get(i).getCharge());
            infoClass.setAvailableTime(list.get(i).getTimeAvailable());
            infoClass.setServiceCategory(list.get(i).getCategoryName());
            infoClass.setServiceName(list.get(i).getServiceName());
            finalist.add(infoClass);

            theMap.addMarker(new MarkerOptions()
                    .title(list.get(i).getServiceName())
                    .position(lng2)
                    .icon(BitmapDescriptorFactory
                            .fromResource(entertainment))
                    .snippet(list.get(i).getFirstName() + " "+ list.get(i).getLastName()) );


            theMap.setOnInfoWindowClickListener(this);
        }

我正在根据列表中的纬度和经度设置我的标记,并将所需的信息添加到类 InfoClass 的 ArrayList 中。 现在,当我设置 setInfoWindowAdapter 并将 finalist 传递给我的 InfoWindowAdapter 实现时。

theMap.setInfoWindowAdapter(new InfoWindow(this, finalist));

我的 InfoWindow 类

public class InfoWindow implements InfoWindowAdapter {

private final View myContentsView;
private LayoutInflater mInflator;
ArrayList<InfoClass> list;

public InfoWindow(Context context, ArrayList<InfoClass> list) {
    //Context context = new MainActivity().getApplicationContext();
    Context context2= context;
    mInflator = LayoutInflater.from(context2);
    this.list= list;
    myContentsView = mInflator.inflate(R.layout.info_view, null);
}


@Override
public View getInfoWindow(Marker marker) {

    return myContentsView;      
}


@Override
public View getInfoContents(Marker arg0) {
    return null;
}   

}

现在在 getInfoWindow 方法中,我如何获取特定标记的信息,因为每个标记需要根据其 Location 显示单独的信息。

【问题讨论】:

    标签: android google-maps-markers google-maps-android-api-2


    【解决方案1】:

    试试这个,

    WindowLayout.xml

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

    MainActivity.java

    public class MainActivity extends FragmentActivity {
    
    
    GoogleMap googleMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    // Getting reference to the SupportMapFragment of activity_main.xml
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    
    // Getting GoogleMap object from the fragment
    googleMap = mapFragment.getMap();
    
    // Setting a custom info window adapter for the google map
    googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {
    
        // Use default InfoWindow frame
        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }
    
        // Defines the contents of the InfoWindow
        @Override
        public View getInfoContents(Marker arg0) {
    
            // Getting view from the layout file info_window_layout
            View v = getLayoutInflater().inflate(R.layout.windowlayout, null);
    
            // Getting the position from the marker
            LatLng latLng = arg0.getPosition();
    
            // Getting reference to the TextView to set latitude
            TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
    
            // Getting reference to the TextView to set longitude
            TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
    
            // Setting the latitude
            tvLat.setText("Latitude:" + latLng.latitude);
    
            // Setting the longitude
            tvLng.setText("Longitude:"+ latLng.longitude);
    
            // Returning the view containing InfoWindow contents
            return v;
    
        }
    });
    
    // Adding and showing marker while touching the GoogleMap
    googleMap.setOnMapClickListener(new OnMapClickListener() {
    
        @Override
        public void onMapClick(LatLng arg0) {
            // Clears any existing markers from the GoogleMap
            googleMap.clear();
    
            // Creating an instance of MarkerOptions to set position
            MarkerOptions markerOptions = new MarkerOptions();
    
            // Setting position on the MarkerOptions
            markerOptions.position(arg0);
    
            // Animating to the currently touched position
            googleMap.animateCamera(CameraUpdateFactory.newLatLng(arg0));
    
            // Adding marker on the GoogleMap
            Marker marker = googleMap.addMarker(markerOptions);
    
            // Showing InfoWindow on the GoogleMap
            marker.showInfoWindow();
    
        }
    });
    
    }
    }
    

    通过这种方式,您可以创建自定义布局并实现您想要的方式。

    如果对你有帮助,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-31
      • 2013-04-28
      • 2013-12-20
      • 2013-09-27
      • 2013-08-19
      • 2013-04-12
      • 2012-03-24
      • 1970-01-01
      相关资源
      最近更新 更多