源码:http://download.csdn.net/detail/lm_zp/9522567
首先配置百度地图相关权限 jar包
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/startAddre_seach"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/title_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/title_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="开始地址"
android:textSize="25sp" />
<TextView
android:id="@+id/city_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="<"
android:textSize="30sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/start_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@drawable/find"
android:hint="请输入起始地址"
android:singleLine="true" />
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消" />
</LinearLayout>
<ListView
android:id="@+id/addre_poi"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
listview item 布局
startaddre_item_layout.xml
<?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" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:src="@drawable/time" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_below="@+id/name"
android:ellipsize="end"
android:lines="1"
android:text="TextView"
android:textColor="#777" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView"
android:textSize="20sp" />
</RelativeLayout>
MainActivity.java
package com.example.mymaps;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;
public class MainActivity extends Activity implements OnClickListener {
private PoiSearch mPoiSearch = null;
private SuggestionSearch mSuggestionSearch = null;
private PoiCitySearchOption poiCitySearchOption = null;
private EditText start_adress;
private View view;
private ListView poi_lv;
private List<PoiSearchResults> list = new ArrayList<PoiSearchResults>();
private MyAdapter_StartAddr adapter = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 注意
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
// 初始化搜索模块,注册搜索事件监听
mPoiSearch = PoiSearch.newInstance();
mPoiSearch.setOnGetPoiSearchResultListener(poiListener);
mSuggestionSearch = SuggestionSearch.newInstance();
start_adress = (EditText) findViewById(R.id.start_et);
poi_lv = (ListView) findViewById(R.id.addre_poi);
cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(this);
city_back = (TextView) findViewById(R.id.city_back);
city_back.setOnClickListener(this);
/*
* poi_lv.setOnItemClickListener(new OnItemClickListener() {
*
* @Override public void onItemClick(AdapterView<?> arg0, View arg1, int
* arg2, long arg3) { Intent intent=new
* Intent(MainActivity.this,CityActivity.class);
*
* intent.putExtra("long", list.get(arg2).getMlongitude());
* intent.putExtra("wei", list.get(arg2).getMlatitude());
* intent.putExtra("address", list.get(arg2).getMaddress());
* intent.putExtra("start", "start"); startActivity(intent); //跳转的时候干掉
* finish(); } });
*/
// 给输入框设置监听
start_adress.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
if (start_adress.getText().toString().equals("")) {
list.clear();
adapter.notifyDataSetChanged();
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
if (cs.length() <= 0) {
return;
}
/**
* 使用建议搜索服务获取建议列表,结果在onSuggestionResult()中更新
*/
mSuggestionSearch
.requestSuggestion((new SuggestionSearchOption())
.keyword(cs.toString()).city("北京"));
poiCitySearchOption = new PoiCitySearchOption().city("北京")
.keyword(start_adress.getText().toString());
mPoiSearch.searchInCity(poiCitySearchOption);
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.city_back:
finish();
break;
case R.id.cancel:// 取消按钮
if (!start_adress.getText().toString().equals("")
&& start_adress.getText().toString() != null) {
start_adress.setText("");
list.clear();
adapter.notifyDataSetChanged();
}
break;
default:
break;
}
}
OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
private String poiname;
private String poiadd;
public void onGetPoiResult(PoiResult result) {
// 获取POI检索结果
if (result == null
|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {// 没有找到检索结果
Toast.makeText(MainActivity.this, "未找到结果", Toast.LENGTH_LONG)
.show();
return;
}
list.clear();
if (result.getAllPoi() == null) {
Toast.makeText(MainActivity.this, "未找到结果,请重新输入111",
Toast.LENGTH_LONG).show();
return;
} else {
for (int i = 0; i < result.getAllPoi().size(); i++) {
poiname = result.getAllPoi().get(i).name;
poiadd = result.getAllPoi().get(i).address;
LatLng poilocation = result.getAllPoi().get(i).location;
if (poilocation != null) {
Double latitude = poilocation.latitude;
Double longitude = poilocation.longitude;
// 实例化一个地理编码查询对象
GeoCoder geoCoder = GeoCoder.newInstance();
// 设置反地理编码位置坐标
ReverseGeoCodeOption op = new ReverseGeoCodeOption();
op.location(poilocation);
// 发起反地理编码请求(经纬度->地址信息)
geoCoder.reverseGeoCode(op);
geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
@Override
public void onGetReverseGeoCodeResult(
ReverseGeoCodeResult arg0) {
poiadd = arg0.getAddress();
}
@Override
public void onGetGeoCodeResult(GeoCodeResult arg0) {
}
});
PoiSearchResults results = new PoiSearchResults(
poiname, poiadd, latitude, longitude);
list.add(results);
System.out.println(list.toString());
} else {
Toast.makeText(MainActivity.this, "未找到结果,请重新输入",
Toast.LENGTH_LONG).show();
}
}
}
adapter = new MyAdapter_StartAddr(MainActivity.this, list);
adapter.notifyDataSetChanged();
poi_lv.setAdapter(adapter);
}
public void onGetPoiDetailResult(PoiDetailResult result) {
}
};
private Button cancel;
private TextView city_back;
public boolean onMapPoiClick(MapPoi poi) {
return false;
}
@Override
public void onDestroy() {
mPoiSearch.destroy();
super.onDestroy();
}
}
百度地图返回的属性 Vo类
PoiSearchResults.java
package com.example.mymaps;
public class PoiSearchResults {
private String mname;
private String maddress;
private Double mlatitude;
private Double mlongitude;
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public String getMaddress() {
return maddress;
}
public void setMaddress(String maddress) {
this.maddress = maddress;
}
public Double getMlatitude() {
return mlatitude;
}
public void setMlatitude(Double mlatitude) {
this.mlatitude = mlatitude;
}
public Double getMlongitude() {
return mlongitude;
}
public void setMlongitude(Double mlongitude) {
this.mlongitude = mlongitude;
}
public PoiSearchResults(String mname, String maddress, Double mlatitude,
Double mlongitude) {
super();
this.mname = mname;
this.maddress = maddress;
this.mlatitude = mlatitude;
this.mlongitude = mlongitude;
}
@Override
public String toString() {
return "PoiSearchResults [mname=" + mname + ", maddress=" + maddress
+ ", mlatitude=" + mlatitude + ", mlongitude=" + mlongitude
+ "]";
}
}
listview适配器
MyAdapter_StartAddr.java
package com.example.mymaps;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter_StartAddr extends BaseAdapter {
private Context context;
private List<PoiSearchResults> list;
public MyAdapter_StartAddr(Context context, List<PoiSearchResults> list) {
super();
this.context = context;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder=new ViewHolder();
convertView=View.inflate(context, R.layout.startaddre_item_layout, null);
holder.address=(TextView) convertView.findViewById(R.id.address);
holder.name=(TextView) convertView.findViewById(R.id.name);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
holder.address.setText(list.get(position).getMaddress());
holder.name.setText(list.get(position).getMname());
return convertView;
}
class ViewHolder{
TextView name,address;
}
}