Android的标志定制信息窗口 [英] android marker custom infowindow

查看:164
本文介绍了Android的标志定制信息窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用谷歌地图V2

我需要显示的ListView (定制的ListView 含图片)定制信息窗口。我试了一下,仅在查看成功了,问题是我不能让 listItemClick 事件。

请帮我该怎么办呢?

  googleMap.setInfoWindowAdapter(新InfoWindowAdapter(){@覆盖
公共查看getInfoWindow(标记为arg0){
    // TODO自动生成方法存根
    返回null;
}@覆盖
公共查看getInfoContents(标记为arg0){
    视图V = getLayoutInflater()膨胀(R.layout.infowindow,NULL);
    尝试{
        的String []名= {梅费尔(D22)共管式公寓,出租,巴黎春天(D22)共管式公寓,出租,巴黎春天(D22)共管式公寓,出租,巴黎春天(D22)共管式公寓,出租};
        Log.d(F,names.toString());
        ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(PropertyMapList.this,R.layout.info_row_view,R.id.textView1,姓名);
        Log.d(D,adapter.getCount()+);
        ListView控件列表=(ListView控件)v.findViewById(R.id.listView1);
        list.setAdapter(适配器);
        list.setOnItemClickListener(新OnItemClickListener(){            @覆盖
            公共无效onItemClick(
                    适配器视图<>为arg0,
                    查看ARG1,ARG2 INT,
                    长ARG3){
                Log.d(位置:ARG2 +);
            }
        });
    }赶上(例外五){
        e.printStackTrace();
    }
    返回伏;
}
});


解决方案

最后我成功了自己的答案与这些$ C $ 3C融合

,你可以找到在游戏商店应用程序

https://play.google。 ?COM /存储/应用/细节ID = com.stp.stproperty&安培; HL = EN

标记的XML文件

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =300dp
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直的android:背景=@绘制/ marker_bg>    < ListView控件
        机器人:ID =@ + ID / ListView1的
        机器人:layout_width =300dp
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginTop =7DP
        机器人:layout_marginLeft =5DP
        机器人:layout_marginBottom =20dp
        机器人:layout_marginRight =5DP>    < /&的ListView GT;< / RelativeLayout的>

和列表视图自定义布局(与Arrow图片文字)

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =90dp
    机器人:方向=垂直
    机器人:重力=center_vertical
    机器人:背景=@绘制/ marker_selecter>    < RelativeLayout的机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT>        <的TextView
            机器人:ID =@ + ID / ProeprtyName
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentLeft =真
            机器人:layout_centerHorizo​​ntal =真
            机器人:layout_marginLeft =5DP
            机器人:layout_toLeftOf =@ + ID / RIGHTARROW
            机器人:文字颜色=@彩色/ list_textcolor
            机器人:TEXTSIZE =16SP/>        < ImageView的
            机器人:ID =@ + ID / RIGHTARROW
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentRight =真
            机器人:layout_marginRight =3DP
            机器人:layout_centerInParent =真
            机器人:SRC =@绘制/ detaildisclosure/>
    < / RelativeLayout的>
< / RelativeLayout的>

标记添加地图

  //设置标志称号这里,有这个称号的帮助下,我们可以轻松地定制和改变lat和长值,图标,WEEL您code
googleMap.addMarker(新的MarkerOptions()
    .title伪(infoTitle.get(指数))
    .POSITION(新经纬度(Double.parseDouble(lanAndLat.get(指数).split(\\\\ $)[0]),Double.parseDouble(lanAndLat.get(指数).split(\\\\ $)[ 1])))
    .icon(BitmapDesc​​riptorFactory.fromBitmap(图标)));

标记设置为onclicklistener:

  googleMap.setOnMarkerClickListener(新OnMarkerClickListener(){    @覆盖
    公共布尔onMarkerClick(标记为arg0){        drawCustomMarker(arg0.getPosition()纬度,arg0.getPosition()经度,arg0.getTitle(),0);
        返回true;
    }
});

声明定义标记为全局变量(要查看清除此并重建它会帮助)

  CustomMarker = getLayoutInflater()膨胀(R.layout.infowindow,NULL);

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

 私人无效drawCustomMarker(双人间纬度,经度双,字符串名称,诠释属性ID){
    尝试{
        INT selectedMarkerIndex = -1; //在选从列表视图,这需要作为选择的标记加以强调
        mapinnerLayout.removeView(CustomMarker);
        googleMap.animateCamera(CameraUpdateFactory.newLatLng(新经纬度(纬度,经度)));        //我传递标题字符串以#分隔值访问        的String [] =名称title.split(\\\\#);
        ArrayList的< MarkerAdapterClass> markerClass =新的ArrayList< MarkerAdapterClass>();
        的for(int i = 0; I< names.length;我++){
            //这是自定义类为我所用,如果你愿意,你可以改变这个类
            MarkerAdapterClass classObj =新MarkerAdapterClass();
            的String []数据=名称[I] .split(\\\\ |);
            INT PID =的Integer.parseInt(数据[1]);
            如果(PID ==属性ID){
                selectedMarkerIndex = I;
            }
            classObj.setpropertyTitle(数据[0]);
            classObj.setid(PID);
            classObj.setitemIndex(的Integer.parseInt(数据[2]));
            markerClass.add(classObj);
        }
        CustomMarker.setVisibility(View.VISIBLE);
        MarkerInfoAdapter适配器=新MarkerInfoAdapter(PropertyMapList.this,markerClass,districtView);
        最终的ListView列表=(ListView控件)CustomMarker.findViewById(R.id.listView1);
        list.setAdapter(适配器);
        如果(selectedMarkerIndex> = 0){
            adapter.setSelected(selectedMarkerIndex);
            list.setSelection(selectedMarkerIndex);
        }
        INT高度= getMarkerInfoHeight(markerClass.size(),清单);
        RelativeLayout.LayoutParams的LayoutParams =新RelativeLayout.LayoutParams(450,高度);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        layoutParams.setMargins(0,mapinnerLayout.getHeight()/ 2 - (高度+ 30),0,0);
        mapinnerLayout.addView(CustomMarker,的LayoutParams);    }赶上(例外五){
        //e.printStackTrace();
    }
}

mapinnerLayout 是一个相对布局(图容器)

我用这来支持10和7英寸平板电脑。的高度和宽度会为设备和纵向到横向变化。我在这里计算的:

 公众诠释getMarkerInfoHeight(INT大小的ListView视图){
    INT高度= 90;
    。INT方向= PropertyMapList.this.getResources()getConfiguration()方向。
    DisplayMetrics displaymetrics =新DisplayMetrics();
    。getWindowManager()getDefaultDisplay()getMetrics(displaymetrics)。
    INT deviceheight = displaymetrics.heightPixels;
    RelativeLayout.LayoutParams LP =新RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.setMargins(5,3,5,12);
    如果(方向== Configuration.ORIENTATION_LANDSCAPE&放大器;&安培; deviceheight< = 700){
        如果(大小大于1){
            高度= 190;
            lp.setMargins(5,3,5,25);
        }
    }其他{
        如果(大小== 2){
            高度= 190;
            lp.setMargins(5,3,5,25);
        }否则如果(大小→2){
            高度= 285;
            lp.setMargins(5,3,5,35);
        }
    }
    view.setLayoutParams(LP);
    返回高度;
}

标记适配器:

 公共类MarkerInfoAdapter延伸BaseAdapter {
    私有静态的ArrayList< MarkerAdapterClass> propertyNames;
    私人LayoutInflater mInflater;
    INT selectedPosition = -1;
    私人的ListView parentAdapter;
    上下文语境;
    公共MarkerInfoAdapter(上下文的背景下,ArrayList的< MarkerAdapterClass>的结果,ListView控件parentAdapter){
        propertyNames =结果;
        this.parentAdapter = parentAdapter;
        mInflater = LayoutInflater.from(上下文);
        this.context =背景;
    }
    @覆盖
    公众诠释的getCount(){
        返回propertyNames.size();
    }    @覆盖
    公共对象的getItem(INT位置){
        返回propertyNames.get(位置);
    }    @覆盖
    众长getItemId(INT位置){
        返回的位置;
    }
    静态类ViewHolder {
        TextView的名称;
        ImageView的形象;
    }
    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
        ViewHolder持有人;
        尝试{
            如果(convertView == NULL)
            {
                convertView = mInflater.inflate(R.layout.marker_row_view,NULL);
                持有人=新ViewHolder();
                holder.Name =(TextView中)convertView.findViewById(R.id.ProeprtyName);
                holder.image =(ImageView的)convertView.findViewById(R.id.RightArrow);
                convertView.setTag(保持器);
            }
            其他
            {
                支架=(ViewHolder)convertView.getTag();
            }
            holder.Name.setText(propertyNames.get(位置).getpropertyTitle());
            如果(selectedPosition ==位置){
                convertView.setBackgroundResource(R.drawable.blue_marker_bg);
                ((的TextView)convertView.findViewById(R.id.ProeprtyName))setTextColor(Color.WHITE)。
                convertView.setMinimumHeight(80);
            }其他{
                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。(新OnClickListener(){                @覆盖
                公共无效的onClick(视图v){
                    的setSelected(位置);
                    如果(context.getClass()。getSimpleName()。等于(PropertyMapList)){
                        ((PropertyListAdaptor)parentAdapter.getAdapter()).setSelected(propertyNames.get(position).getitemIndex());
                    }其他{
                        ((FavoriteListAdapter)parentAdapter.getAdapter()).setSelected(propertyNames.get(position).getitemIndex());
                    }
                    parentAdapter.setSelection(propertyNames.get(位置).getitemIndex());
                }
            });
        }赶上(例外五){
            e.printStackTrace();
        }        返回convertView;
    }
    公共无效的setSelected(INT位置){
        selectedPosition =位置;
        notifyDataSetChanged();
    }
}

标记适配器类

 公共类MarkerAdapterClass {
    私人诠释身份证;
    私人字符串propertyTitle;
    私人INT的ItemIndex = 0;    公共字符串getpropertyTitle(){
        返回propertyTitle;
    }
    公共无效setpropertyTitle(字符串名称){
        this.propertyTitle =称号;
    }
    公众诠释getitemIndex(){
        返回的ItemIndex;
    }
    公共无效setitemIndex(INT的ItemIndex){
        this.itemIndex =的ItemIndex;
    }
    公众诠释的getId(){
        返回this.id;
    }
    公共无效SETID(INT ID){
        this.id = ID;
    }
}

I am using Google Map V2

I need to show the ListView (custom ListView with Image) in custom InfoWindow. I tried it and got success only in View, the problem is i can't get the listItemClick event.

Please help me how to do it?

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

解决方案

Finally i got successful answer myself with these code integration

And you can find the app in play-store

https://play.google.com/store/apps/details?id=com.stp.stproperty&hl=en

Marker XML file

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

And the listview custom layout (Text with Arrow image)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="90dp"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:background="@drawable/marker_selecter" >

    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/ProeprtyName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="5dp"
            android:layout_toLeftOf="@+id/RightArrow"
            android:textColor="@color/list_textcolor"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/RightArrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="3dp"
            android:layout_centerInParent="true"
            android:src="@drawable/detaildisclosure" />
    </RelativeLayout>
</RelativeLayout>

Adding marker to map

// Set title for marker here, with the help of this title we can customize easily and change lat and long values, icon as weel to your code
googleMap.addMarker(new MarkerOptions()
    .title(infoTitle.get(index))
    .position(new LatLng(Double.parseDouble(lanAndLat.get(index).split("\\$")[0]), Double.parseDouble(lanAndLat.get(index).split("\\$")[1])))
    .icon(BitmapDescriptorFactory.fromBitmap(icon)));

Set marker onclicklistener as :

googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {

    @Override
    public boolean onMarkerClick(Marker arg0) {

        drawCustomMarker(arg0.getPosition().latitude, arg0.getPosition().longitude, arg0.getTitle(),0);
        return true;
    }
});

Declare the custom marker as global variable (To clear this from view and recreate it will help)

CustomMarker = getLayoutInflater().inflate(R.layout.infowindow, null);

Custom marker drawing function. I made it for my use case. Please customize it according to your requirement

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

mapinnerLayout is an relative layout (container of map)

I am using this to support 10 and 7 inches tablet. The height and width will vary for devices and portrait to landscape. I am calculating this here:

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

Marker adapter:

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

Marker adapter class

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

这篇关于Android的标志定制信息窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆