Android标记自定义信息窗口

Android标记自定义信息窗口,android,google-maps-android-api-2,marker,infowindow,Android,Google Maps Android Api 2,Marker,Infowindow,我正在使用谷歌地图V2 我需要在customInfoWindow中显示ListView(带有图像的customListView)。我尝试了它,只在视图中获得了成功,问题是我无法获得列表项单击事件 googleMap.setInfoWindowAdapter(new InfoWindowAdapter() { @Override public View getInfoWindow(Marker arg0) { // TODO Auto-generated method stub

我正在使用谷歌地图V2

我需要在custom
InfoWindow
中显示
ListView
(带有图像的custom
ListView
)。我尝试了它,只在
视图
中获得了成功,问题是我无法获得
列表项单击
事件

googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {

@Override
public View getInfoWindow(Marker arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public View getInfoContents(Marker arg0) {
    View v = getLayoutInflater().inflate(R.layout.infowindow, null);
    try{
        String[] names = {"The Mayfair (D22) Condominium, For Rent","The Mayfair (D22) Condominium, For Rent","The Mayfair (D22) Condominium, For Rent","The Mayfair (D22) Condominium, For Rent"};
        Log.d("f", names.toString());
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(PropertyMapList.this, R.layout.info_row_view, R.id.textView1, names);
        Log.d("d", adapter.getCount()+"");
        ListView list = (ListView) v.findViewById(R.id.listView1);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(
                    AdapterView<?> arg0,
                    View arg1, int arg2,
                    long arg3) {
                Log.d("position:", arg2+"");
            }
        });
    }catch (Exception e) {
        e.printStackTrace();
    }
    return v;
}
});
googleMap.setInfoWindowAdapter(新的InfoWindowAdapter(){
@凌驾
公共视图getInfoWindow(标记arg0){
//TODO自动生成的方法存根
返回null;
}
@凌驾
公共视图getInfoContents(标记arg0){
视图v=GetLayoutFlater().充气(R.layout.infowindow,null);
试一试{
字符串[]名称={“Mayfair(D22)公寓出租”、“Mayfair(D22)公寓出租”、“Mayfair(D22)公寓出租”、“Mayfair(D22)公寓出租”};
Log.d(“f”,names.toString());
ArrayAdapter=新的ArrayAdapter(PropertyMapList.this,R.layout.info\u row\u视图,R.id.textView1,名称);
Log.d(“d”,adapter.getCount()+);
ListView列表=(ListView)v.findViewById(R.id.listView1);
list.setAdapter(适配器);
list.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共空间(
AdapterView arg0,
视图arg1,int arg2,
长arg3){
Log.d(“位置:,arg2+”);
}
});
}捕获(例外e){
e、 printStackTrace();
}
返回v;
}
});

绘制的信息窗口不是实时视图。返回视图时,该视图被渲染为图像(使用
view.draw(Canvas)


检查

最后,通过代码集成,我自己得到了一个成功的答案

您可以在Play Store中找到该应用程序

标记XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:orientation="vertical" android:background="@drawable/marker_bg" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="7dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="5dp" >

    </ListView>

</RelativeLayout>
将标记onclicklistener设置为:

googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {

    @Override
    public boolean onMarkerClick(Marker arg0) {

        drawCustomMarker(arg0.getPosition().latitude, arg0.getPosition().longitude, arg0.getTitle(),0);
        return true;
    }
});
将自定义标记声明为全局变量(将其从视图中清除并重新创建将有所帮助)

自定义标记绘图功能。我为我的用例做的。请根据您的要求定制

private void drawCustomMarker(Double latitude, Double longitude, String title, int propertyId){
    try{
        int selectedMarkerIndex = -1; // On-select from list-view this needs to be highlighted in marker as selected
        mapinnerLayout.removeView(CustomMarker);
        googleMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)));

        // I am passing title as string with # separated values to access

        String[] names = title.split("\\#");
        ArrayList<MarkerAdapterClass> markerClass = new ArrayList<MarkerAdapterClass>();
        for(int i=0;i<names.length;i++){
            // this is custom class for my own use if you want you can change this class 
            MarkerAdapterClass classObj = new MarkerAdapterClass();
            String[] data = names[i].split("\\|");
            int pId = Integer.parseInt(data[1]);
            if(pId==propertyId){
                selectedMarkerIndex = i;
            }
            classObj.setpropertyTitle(data[0]);
            classObj.setid(pId);
            classObj.setitemIndex(Integer.parseInt(data[2]));
            markerClass.add(classObj);
        }
        CustomMarker.setVisibility(View.VISIBLE);
        MarkerInfoAdapter adapter = new MarkerInfoAdapter(PropertyMapList.this, markerClass, districtView);
        final ListView list = (ListView) CustomMarker.findViewById(R.id.listView1);
        list.setAdapter(adapter);
        if(selectedMarkerIndex>=0){
            adapter.setSelected(selectedMarkerIndex);
            list.setSelection(selectedMarkerIndex);
        }
        int height = getMarkerInfoHeight(markerClass.size(), list);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(450,height);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        layoutParams.setMargins(0, mapinnerLayout.getHeight()/2 - (height+30), 0, 0);
        mapinnerLayout.addView(CustomMarker, layoutParams);

    }catch(Exception e){
        //e.printStackTrace();
    }
}
标记适配器:

public class MarkerInfoAdapter extends BaseAdapter {
    private static ArrayList<MarkerAdapterClass> propertyNames;
    private LayoutInflater mInflater;
    int selectedPosition = -1;
    private ListView parentAdapter;
    Context context;
    public MarkerInfoAdapter(Context context, ArrayList<MarkerAdapterClass> results, ListView parentAdapter) {
        propertyNames = results;
        this.parentAdapter = parentAdapter;
        mInflater = LayoutInflater.from(context);
        this.context = context;
    }
    @Override
    public int getCount() {
        return propertyNames.size();
    }

    @Override
    public Object getItem(int position) {
        return propertyNames.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    static class ViewHolder {
        TextView Name;
        ImageView image;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        try{
            if(convertView==null)
            {
                convertView=mInflater.inflate(R.layout.marker_row_view, null);
                holder=new ViewHolder();
                holder.Name=(TextView)convertView.findViewById(R.id.ProeprtyName);
                holder.image=(ImageView)convertView.findViewById(R.id.RightArrow);
                convertView.setTag(holder);
            }
            else
            {
                holder=(ViewHolder)convertView.getTag();
            }
            holder.Name.setText(propertyNames.get(position).getpropertyTitle());
            if(selectedPosition == position){
                convertView.setBackgroundResource(R.drawable.blue_marker_bg);
                ((TextView)convertView.findViewById(R.id.ProeprtyName)).setTextColor(Color.WHITE);
                convertView.setMinimumHeight(80);
            }else{
                convertView.setBackgroundResource(R.drawable.directories_list_bg);
                ((TextView)convertView.findViewById(R.id.ProeprtyName)).setTextColor(Color.BLACK);
                convertView.setMinimumHeight(80);
            }
            ((TextView)convertView.findViewById(R.id.ProeprtyName)).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    setSelected(position);
                    if(context.getClass().getSimpleName().equals("PropertyMapList")){
                        ((PropertyListAdaptor)parentAdapter.getAdapter()).setSelected(propertyNames.get(position).getitemIndex());
                    }else{
                        ((FavoriteListAdapter)parentAdapter.getAdapter()).setSelected(propertyNames.get(position).getitemIndex());
                    }
                    parentAdapter.setSelection(propertyNames.get(position).getitemIndex());
                }
            });
        }catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }
    public void setSelected(int position) {
        selectedPosition = position;
        notifyDataSetChanged();
    }
}

s、 我看过这个医生,但我非常需要像我在这里试过的那样做。可能吗?@SampathKumar不可能。您可以改为在单击标记时显示对话框。
CustomMarker = getLayoutInflater().inflate(R.layout.infowindow, null);
private void drawCustomMarker(Double latitude, Double longitude, String title, int propertyId){
    try{
        int selectedMarkerIndex = -1; // On-select from list-view this needs to be highlighted in marker as selected
        mapinnerLayout.removeView(CustomMarker);
        googleMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)));

        // I am passing title as string with # separated values to access

        String[] names = title.split("\\#");
        ArrayList<MarkerAdapterClass> markerClass = new ArrayList<MarkerAdapterClass>();
        for(int i=0;i<names.length;i++){
            // this is custom class for my own use if you want you can change this class 
            MarkerAdapterClass classObj = new MarkerAdapterClass();
            String[] data = names[i].split("\\|");
            int pId = Integer.parseInt(data[1]);
            if(pId==propertyId){
                selectedMarkerIndex = i;
            }
            classObj.setpropertyTitle(data[0]);
            classObj.setid(pId);
            classObj.setitemIndex(Integer.parseInt(data[2]));
            markerClass.add(classObj);
        }
        CustomMarker.setVisibility(View.VISIBLE);
        MarkerInfoAdapter adapter = new MarkerInfoAdapter(PropertyMapList.this, markerClass, districtView);
        final ListView list = (ListView) CustomMarker.findViewById(R.id.listView1);
        list.setAdapter(adapter);
        if(selectedMarkerIndex>=0){
            adapter.setSelected(selectedMarkerIndex);
            list.setSelection(selectedMarkerIndex);
        }
        int height = getMarkerInfoHeight(markerClass.size(), list);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(450,height);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        layoutParams.setMargins(0, mapinnerLayout.getHeight()/2 - (height+30), 0, 0);
        mapinnerLayout.addView(CustomMarker, layoutParams);

    }catch(Exception e){
        //e.printStackTrace();
    }
}
public int getMarkerInfoHeight(int size, ListView view){
    int height = 90;
    int Orientation = PropertyMapList.this.getResources().getConfiguration().orientation;
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int deviceheight = displaymetrics.heightPixels;
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.setMargins(5, 3, 5, 12);
    if(Orientation == Configuration.ORIENTATION_LANDSCAPE && deviceheight <= 700){
        if(size > 1){
            height = 190;
            lp.setMargins(5, 3, 5, 25);
        }
    }else{
        if(size == 2){
            height = 190;
            lp.setMargins(5, 3, 5, 25);
        }else if(size > 2){
            height = 285;
            lp.setMargins(5, 3, 5, 35);
        }
    }
    view.setLayoutParams(lp);
    return height;
}
public class MarkerInfoAdapter extends BaseAdapter {
    private static ArrayList<MarkerAdapterClass> propertyNames;
    private LayoutInflater mInflater;
    int selectedPosition = -1;
    private ListView parentAdapter;
    Context context;
    public MarkerInfoAdapter(Context context, ArrayList<MarkerAdapterClass> results, ListView parentAdapter) {
        propertyNames = results;
        this.parentAdapter = parentAdapter;
        mInflater = LayoutInflater.from(context);
        this.context = context;
    }
    @Override
    public int getCount() {
        return propertyNames.size();
    }

    @Override
    public Object getItem(int position) {
        return propertyNames.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    static class ViewHolder {
        TextView Name;
        ImageView image;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        try{
            if(convertView==null)
            {
                convertView=mInflater.inflate(R.layout.marker_row_view, null);
                holder=new ViewHolder();
                holder.Name=(TextView)convertView.findViewById(R.id.ProeprtyName);
                holder.image=(ImageView)convertView.findViewById(R.id.RightArrow);
                convertView.setTag(holder);
            }
            else
            {
                holder=(ViewHolder)convertView.getTag();
            }
            holder.Name.setText(propertyNames.get(position).getpropertyTitle());
            if(selectedPosition == position){
                convertView.setBackgroundResource(R.drawable.blue_marker_bg);
                ((TextView)convertView.findViewById(R.id.ProeprtyName)).setTextColor(Color.WHITE);
                convertView.setMinimumHeight(80);
            }else{
                convertView.setBackgroundResource(R.drawable.directories_list_bg);
                ((TextView)convertView.findViewById(R.id.ProeprtyName)).setTextColor(Color.BLACK);
                convertView.setMinimumHeight(80);
            }
            ((TextView)convertView.findViewById(R.id.ProeprtyName)).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    setSelected(position);
                    if(context.getClass().getSimpleName().equals("PropertyMapList")){
                        ((PropertyListAdaptor)parentAdapter.getAdapter()).setSelected(propertyNames.get(position).getitemIndex());
                    }else{
                        ((FavoriteListAdapter)parentAdapter.getAdapter()).setSelected(propertyNames.get(position).getitemIndex());
                    }
                    parentAdapter.setSelection(propertyNames.get(position).getitemIndex());
                }
            });
        }catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }
    public void setSelected(int position) {
        selectedPosition = position;
        notifyDataSetChanged();
    }
}
public class MarkerAdapterClass {
    private int id;
    private String propertyTitle;
    private int itemIndex=0;

    public String getpropertyTitle() {
        return propertyTitle;
    }
    public void setpropertyTitle(String title) {
        this.propertyTitle = title;
    }
    public int getitemIndex() {
        return itemIndex;
    }
    public void setitemIndex(int itemIndex) {
        this.itemIndex = itemIndex;
    }
    public int getid() {
        return this.id;
    }
    public void setid(int id) {
        this.id = id;
    }
}