应用场景:输入地址信息,以listview的方式显示周边信息
(需求也是够变态的,都改了n次了,后台不懂总是要我走前面提到的关键字搜索,日了狗了)
思路:edittext TextWatcher侦听,将文字转化为经纬度
public void getLatlon(final String name) {
GeocodeQuery query = new GeocodeQuery(name, "010");// 第一个参数表示地址,第二个参数表示查询城市,中文或者中文全拼 010表示北京
geocoderSearch.getFromLocationNameAsyn(query);// 设置同步地理编码请求
}
然后在onGeocodeSearched方法中doSearchQuery查询
即:
protected void doSearchQuery(LatLonPoint latLonPoint) {
currentPage = 0;
query = new PoiSearch.Query(text.getText().toString(), "", "010");// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索 区域(空字符串代表全国)
query.setPageSize(20);// 设置每页最多返回多少条poiitem
query.setPageNum(currentPage);// 设置查第一页
query.setLimitDiscount(false);
query.setLimitGroupbuy(false);
poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.setBound(new SearchBound(latLonPoint, 2000, true));//
poiSearch.searchPOIAsyn();
}
在重写的方法中:
@Override
public void onPoiSearched(PoiResult result, int rCode) {
// TODO Auto-generated method stub
ArrayList<PoiItem> pois = result.getPois();
ArrayList<String> address=new ArrayList<String>();
for(int i=0;i<pois.size();i++){
// Log.v("result.getPois()", result.getPois().get(i)+"");
address.add(result.getPois().get(i).toString());
}
mList=new ArrayList<Map<String,Object>>();
for (int i = 0; i < address.size(); i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put(KEY[0], R.drawable.iv_daoh);// 加入图片
map.put(KEY[1], address.get(i));// 文字
map.put(KEY[2], R.drawable.iv_come);
mList.add(map);
}
adapter = new SearchAdapter(MainActivity.this);
adapter.setDataSource(mList);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
好了,当然还有很多没处理,比如listview的刷新,以及清空,以及其他图片的点击事件,当然这都不是重点,下面上完整代码以及代码的下载链接:
mainactivity:
public class MainActivity extends Activity implements OnGeocodeSearchListener,OnPoiSearchListener,TextWatcher{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
text = (EditText) findViewById(R.id.txtfind);
text.addTextChangedListener(this);
button = (ImageView) findViewById(R.id.iv_search);
geocoderSearch = new GeocodeSearch(this);
geocoderSearch.setOnGeocodeSearchListener(this);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getLatlon(text.getText().toString());
}
});
}
/**
* 开始进行poi搜索
*/
private ListView lv;
private int currentPage = 0;// 当前页面,从0开始计数
private PoiSearch.Query query;// Poi查询条件类
private PoiSearch poiSearch;// POI搜索
protected void doSearchQuery(LatLonPoint latLonPoint) {
currentPage = 0;
query = new PoiSearch.Query(text.getText().toString(), "", "010");// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
query.setPageSize(20);// 设置每页最多返回多少条poiitem
query.setPageNum(currentPage);// 设置查第一页
query.setLimitDiscount(false);
query.setLimitGroupbuy(false);
poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.setBound(new SearchBound(latLonPoint, 200, true));//
poiSearch.searchPOIAsyn();
}
/**
* 响应地理编码
*/
private GeocodeSearch geocoderSearch;
private EditText text;
private ImageView button;
private String addressName;
public void getLatlon(final String name) {
GeocodeQuery query = new GeocodeQuery(name, "010");// 第一个参数表示地址,第二个参数表示查询城市,中文或者中文全拼,citycode、adcode,
geocoderSearch.getFromLocationNameAsyn(query);// 设置同步地理编码请求
}
/**
* 地理编码查询回调
*/
@Override
public void onGeocodeSearched(GeocodeResult result, int rCode) {
if (rCode == 0) {
if (result != null && result.getGeocodeAddressList() != null
&& result.getGeocodeAddressList().size() > 0) {
GeocodeAddress address = result.getGeocodeAddressList().get(0);
addressName = "经纬度值:" + address.getLatLonPoint() + "\n位置描述:"
+ address.getFormatAddress();
ToastUtil.show(MainActivity.this, addressName);
// getAddress(address.getLatLonPoint());
doSearchQuery(address.getLatLonPoint());
} else {
ToastUtil.show(MainActivity.this, R.string.no_result);
}
} else if (rCode == 27) {
ToastUtil.show(MainActivity.this, R.string.error_network);
} else if (rCode == 32) {
ToastUtil.show(MainActivity.this, R.string.error_key);
} else {
ToastUtil.show(MainActivity.this,
getString(R.string.error_other) + rCode);
}
}
/**
* 响应逆地理编码
*/
public void getAddress(final LatLonPoint latLonPoint) {
RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200,
GeocodeSearch.AMAP);// 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
geocoderSearch.getFromLocationAsyn(query);// 设置同步逆地理编码请求
}
/**
* 将经纬度转化为地址值,需传递经纬度
*/
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
if (rCode == 0) {
if (result != null && result.getRegeocodeAddress() != null
&& result.getRegeocodeAddress().getFormatAddress() != null) {
addressName = result.getRegeocodeAddress().getFormatAddress()
+ "附近";
ToastUtil.show(MainActivity.this, addressName);
Log.v("附近的值是:::", addressName);
} else {
ToastUtil.show(MainActivity.this, R.string.no_result);
}
} else if (rCode == 27) {
ToastUtil.show(MainActivity.this, R.string.error_network);
} else if (rCode == 32) {
ToastUtil.show(MainActivity.this, R.string.error_key);
} else {
ToastUtil.show(MainActivity.this,
getString(R.string.error_other) + rCode);
}
}
/**
* POI详情回调
*/
@Override
public void onPoiItemDetailSearched(PoiItemDetail result, int rCode) {
}
/**
* 地图处理marker
*/
public static final String KEY[] = new String[] { "ivDaoH", "txtFind",
"ivCome" };
private SearchAdapter adapter;
ArrayList<Map<String, Object>> mList;
@Override
public void onPoiSearched(PoiResult result, int rCode) {
// TODO Auto-generated method stub
ArrayList<PoiItem> pois = result.getPois();
ArrayList<String> address=new ArrayList<String>();
for(int i=0;i<pois.size();i++){
address.add(result.getPois().get(i).toString());
}
mList=new ArrayList<Map<String,Object>>();
for (int i = 0; i < address.size(); i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put(KEY[0], R.drawable.iv_daoh);// 加入图片
map.put(KEY[1], address.get(i));// 文字
map.put(KEY[2], R.drawable.iv_come);
mList.add(map);
}
adapter = new SearchAdapter(MainActivity.this);
adapter.setDataSource(mList);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
getLatlon(s.toString());
}
}
完整代码见:
http://download.csdn.net/detail/onebelowzero2012/9388043
注意替换自己的sha1和key值
另外:关于前面
http://www.cnblogs.com/yizuochengchi2012/p/5044039.html的日历控件,其他手机上没问题,华为手机一点,下面的三个textview立马跑到中间去,很诡异啊,蛋疼中。。。