Android高德地图检索显示

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.seventc.shisannong.xiyi.R;
import com.seventc.shisannong.xiyi.adapter.SeachAddressAdapter;
import com.seventc.shisannong.xiyi.app.MyAppliaction;
import com.seventc.shisannong.xiyi.entity.AddressBean;

import java.util.ArrayList;

public class SearchAddressActivity extends Activity implements PoiSearch.OnPoiSearchListener,View.OnClickListener{
    private Context mContext;
    private EditText et_search;
    private RelativeLayout rl_finish;
    private ListView lv_address;
    private PoiSearch.Query query;
    private int page=0;
    private PoiSearch poiSearch;
    private SeachAddressAdapter adapter;
    private ArrayList<AddressBean> data = new ArrayList<AddressBean>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_address);
        MyAppliaction.setInnerLayoutFullScreen(this);
        MyAppliaction.addActivity(this);
        mContext=this;
        initView();
        setOnClick();
    }

    private void initView() {
        //String CityName=mapUtils.getCityName();
        // Log.i("=====CityName",CityName);
        adapter=new SeachAddressAdapter(mContext,data);
        et_search= (EditText) findViewById(R.id.et_search);
        rl_finish= (RelativeLayout) findViewById(R.id.rl_finish);
        lv_address= (ListView) findViewById(R.id.lv_address);
        lv_address.setAdapter(adapter);
    }

    private void seach(String address){
        //keyWord表示搜索字符串,
        //第二个参数表示POI搜索类型,二者选填其一,选用POI搜索类型时建议填写类型代码,码表可以参考下方(而非文字)
        //cityCode表示POI搜索区域,可以是城市编码也可以是城市名称,也可以传空字符串,空字符串代表全国在全国范围内进行搜索
        query=new PoiSearch.Query(address,"",MyAppliaction.getCityName());
        query.setPageSize(300);// 设置每页最多返回多少条poiitem
        query.setPageNum(page);//设置查询页码

        poiSearch = new PoiSearch(this, query);
        poiSearch.searchPOIAsyn();//调用 PoiSearch 的 searchPOIAsyn() 方法发送请求。

        poiSearch.setOnPoiSearchListener(this);

        //周边检索
        //   poiSearch.setBound(new SearchBound(new LatLonPoint(locationMarker.getPosition().latitude,
        //          locationMarker.getPosition().longitude), 1000));//设置周边搜索的中心点以及半径
    }

    private void setOnClick() {
        rl_finish.setOnClickListener(this);
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String str=et_search.getText().toString();
                if (str.length()>0){
                    seach(str);
                }
            }
        });

        lv_address.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent();//ListView item点击回调
                intent.putExtra("data",data.get(i));
                setResult(100,intent);
                finish();
            }
        });

    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        data.clear();
        //解析result获取POI信息
        if(i == 1000 && poiResult != null) {
            ArrayList<PoiItem> items = poiResult.getPois();
            for (PoiItem item : items) {
                //获取经纬度对象
                LatLonPoint llp = item.getLatLonPoint();
                double lon = llp.getLongitude();
                double lat = llp.getLatitude();

                //获取标题
                String title = item.getTitle();
                //获取内容
                String text = item.getSnippet();
                String name=item.getAdName();
                String cityName=item.getCityName();
                String area=item.getBusinessArea();
                String  provinceName=item.getProvinceName();

                String addressInfo=provinceName+cityName+name+text;

                data.add(new AddressBean(lon, lat, title, text,addressInfo));
            }
            adapter.refreshData(data);
        }
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_finish:
                finish();
                break;
        }
    }

}

//Activity布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    >

    <View
        android:id="@+id/v"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/zhuti_color2"/>
    <RelativeLayout
        android:id="@+id/RelativeLayoutbar"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:background="@color/white"
        android:layout_below="@+id/v"
        >

        <RelativeLayout
            android:id="@+id/rl_finish"
            android:layout_width="60dp"
            android:layout_height="match_parent"
            android:visibility="visible">
            <Button
                android:clickable="@null"
                android:id="@+id/tv_left_button"
                android:layout_width="20dp"
                android:layout_height="25dp"
                android:layout_marginLeft="10dp"
                android:layout_centerVertical="true"
                android:background="@mipmap/back"
                android:textColor="#0678e9"
                android:textSize="15sp" />
        </RelativeLayout>

        <EditText
            android:id="@+id/et_search"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_centerInParent="true"
            android:textColor="@color/black3"
            android:textSize="14sp"
            android:drawableLeft="@mipmap/sousuo1"
            android:paddingLeft="10dp"
            android:paddingRight="8dp"
            android:drawablePadding="8dp"
            android:layout_marginRight="20dp"
            android:layout_toRightOf="@+id/rl_finish"
            android:background="@drawable/shape_address"
            android:hint="小区/写字楼/学校"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="@color/line3"
            android:layout_alignParentBottom="true"/>

    </RelativeLayout>

    <ListView
        android:id="@+id/lv_address"
        android:layout_width="match_parent"
        android:scrollbars="none"
        android:background="@color/white"
        android:layout_below="@+id/RelativeLayoutbar"
        android:layout_height="wrap_content"></ListView>

</RelativeLayout>


//SeachAddressAdapter

public class SeachAddressAdapter extends BaseAdapter{
    private Context mContext;
    private ArrayList<AddressBean> list;
    private LayoutInflater inflater;

    public SeachAddressAdapter(Context mContext, ArrayList<AddressBean> list) {
        this.mContext = mContext;
        this.list = list;
        this.inflater=LayoutInflater.from(mContext);
    }

    public void refreshData( ArrayList<AddressBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if (view==null){
            holder=new ViewHolder();
            view=inflater.inflate(R.layout.item_seach_address,null);
            holder.tv_name= (TextView) view.findViewById(R.id.tv_name);
            holder.tv_address= (TextView) view.findViewById(R.id.tv_address);
            view.setTag(holder);
        }else {
            holder= (ViewHolder) view.getTag();
        }

        AddressBean bean=list.get(i);
        holder.tv_name.setText(bean.getTitle());
        holder.tv_address.setText(bean.getAddress());

        return view;
    }

    class ViewHolder{
        TextView tv_name,tv_address;
    }
}
//adapter item布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp">
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black3"
        android:textSize="16sp"
        android:text=""/>
    <TextView
        android:id="@+id/tv_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:layout_marginTop="5dp"
        android:text=""/>
</LinearLayout>

//AddressBean

public class AddressBean implements Serializable{
    private double longitude;//经度
    private double latitude;//纬度
    private String title;//信息标题
    private String text;//信息内容
    private String address;

    public AddressBean(double longitude, double latitude, String title, String text, String address) {
        this.longitude = longitude;
        this.latitude = latitude;
        this.title = title;
        this.text = text;
        this.address = address;
    }

    public double getLongitude() {
        return longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public String getTitle() {
        return title;
    }

    public String getText() {
        return text;
    }

    public String getAddress() {
        return address;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_27400335/article/details/79413183