一、定位
1、LocationClient设置定位监听和属性,位置信息在BDLocation类中
public LocationClient mLocationClient = null;
public MyLocationListener myListener;
mLocationClient = new LocationClient(this); //声明LocationClient类
mLocationClient.registerLocationListener(myListener); //注册监听函数
mLocationClient.start();//启动定位
private void setCenter(BDLocation bdLocation) {
// 开启定位图层
mapManager.setMyLocationEnabled(true);
// 构造定位数据
MyLocationData locData = new MyLocationData.Builder()
.accuracy(bdLocation.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(bdLocation.getLatitude())
.longitude(bdLocation.getLongitude()).build();
// 设置定位数据
mapManager.setMyLocationData(locData);
//定义地图状态
LatLng mypos = new LatLng(lat,lon);
MapStatus mMapStatus = new MapStatus.Builder()
.target(mypos)
.zoom(18)//缩放等级
.build();
//定义MapStatusUpdate对象,以便描述地图状态将要发生的变化
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
//改变地图状态
mapManager.setMapStatus(mMapStatusUpdate);
}
/**
* 内部类,监听定位信息
*/
class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
//判断定位成功或失败,以及定位类型
String addr = bdLocation.getAddrStr(); //获取详细地址信息
String country = bdLocation.getCountry(); //获取国家
String province = bdLocation.getProvince(); //获取省份
String district = bdLocation.getDistrict(); //获取区县
String street = bdLocation.getStreet(); //获取街道信息
String locationDescribe = bdLocation.getLocationDescribe(); //获取位置描述信息
lat = bdLocation.getLatitude();
lon = bdLocation.getLongitude();
city = bdLocation.getCity(); //获取城市
//第一次定位需要更新下地图显示状态
if (isFirstLoc) {
isFirstLoc = false;
setCenter(bdLocation);
}
}
}
2、LocationClient设置定位常用属性,需将配置好的LocationClientOption对象,通过setLocOption方法传递给LocationClient对象使用
//配置定位SDK参数
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
//可选,设置定位模式,默认高精度
//LocationMode.Hight_Accuracy:高精度;
//LocationMode. Battery_Saving:低功耗;
//LocationMode. Device_Sensors:仅使用设备;
option.setCoorType("bd09ll");
//可选,设置返回经纬度坐标类型,默认gcj02
//gcj02:国测局坐标;
//bd09ll:百度经纬度坐标;
//bd09:百度墨卡托坐标;
//海外地区定位,无需设置坐标类型,统一返回wgs84类型坐标
option.setScanSpan(1000);
//可选,设置发起定位请求的间隔,int类型,单位ms
//如果设置为0,则代表单次定位,即仅定位一次,默认为0
//如果设置非0,需设置1000ms以上才有效
option.setPriority(LocationClientOption.GpsFirst);
option.setOpenGps(true);
//可选,设置是否使用gps,默认false
//使用高精度和仅用设备两种定位模式的,参数必须设置为true
option.setLocationNotify(true);
//可选,设置是否当GPS有效时按照1S/1次频率输出GPS结果,默认false
option.setIgnoreKillProcess(false);
//可选,定位SDK内部是一个service,并放到了独立进程。
//设置是否在stop的时候杀死这个进程,默认(建议)不杀死,即setIgnoreKillProcess(true)
option.SetIgnoreCacheException(false);
//可选,设置是否收集Crash信息,默认收集,即参数为false
option.setWifiCacheTimeOut(5*60*1000);
//可选,7.2版本新增能力
//如果设置了该接口,首次启动定位时,会先判断当前WiFi是否超出有效期,若超出有效期,会先重新扫描WiFi,然后定位
option.setEnableSimulateGps(false);
//可选,设置是否需要过滤GPS仿真结果,默认需要,即参数为false
option.setIsNeedAddress(true);
//允许获得地址信息(默认是只有经纬度)
3、以上定位位置使用默认的图标,也可以通过自定义
//构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions()
.position(location)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.dwicon));
//在地图上添加Marker,并显示
Marker marker = (Marker) mapManager.addOverlay(option);
二、热词建议搜索
1、热词建议搜索思路
通过监听edittext的内容变化进行搜索,通过listview显示搜索的结果
2、相关类
SuggestionSearch,搜索执行对象
onGetSuggestionResult,搜索结果回调
SuggestionSearchOption,搜索参数设置
SuggestionResult,搜索结果保存
3、步骤
private SuggestionSearch mysearch;
mysearch = SuggestionSearch.newInstance();//单例模式
mysearch.setOnGetSuggestionResultListener(this);
//建议搜索结果回调
@Override
public void onGetSuggestionResult(SuggestionResult suggestionResult) {
if (suggestionResult == null || suggestionResult.getAllSuggestions() == null) {
return;
}
for (SuggestionResult.SuggestionInfo info : suggestionResult.getAllSuggestions()) {
if (info.key != null){
MyPoiResult poiResult=new MyPoiResult();
//记录搜索到的城市,名称和地区;
poiResult.setCity(info.city);
poiResult.setName(info.key);
poiResult.setAddress(info.city+info.district+info.address);
System.out.println(info.city+info.district+info.address);
mPoilist.add(poiResult);
}
}
mPoiListAdapter.updateListView(mPoilist,content);//更新listview
}
/*监听文字的变化*/
et_search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//回调进行建议搜索
if (!(sCallBack == null)){//sCallBack是一个接口,用于实现搜索
sCallBack.suggestAciton(et_search.getText().toString());
}
}
});
searchView.setOnClickSuggest(new sCallBack() {
@Override
public void suggestAciton(String string) {
if(string.equals("")||string==null){//没有输入也为空
mPoiListAdapter.updateListView(mPoilist,null);//更新listview
return;
}
//根据输入的名称和城市进行建议搜索,建议搜索很强大,即使固定了城市,也可以搜索到别的地方。
mysearch.requestSuggestion((new SuggestionSearchOption()).keyword(string).city(city));
}
});
private PoiListAdapter mPoiListAdapter=null; //自定义的ListView适配器对象。
private List mPoilist=null; //查询后的结果数据链表;其中MrPoiResult是自定义的Bean对象。
mPoiListAdapter=new PoiListAdapter(this, mPoilist);
searlistview.setAdapter(mPoiListAdapter);
适配器的代码如下:
public class PoiListAdapter extends AdapterBase{
private Context ct;
private ViewHolder viewHolder=null;
private String mSearchKey; //搜索的关键字对象;
public PoiListAdapter(Context ct,List postions){
this.ct=ct;
appendToList(postions);
}
public void updateListView(List datas,String searchKey){
mSearchKey=searchKey;
clear();
appendToList(datas);
notifyDataSetChanged();//通知数据发生变化
}
@Override
protected View getExView(int position, View convertView, ViewGroup parent) {
MyPoiResult poiResult=getList().get(position);
//构造item项样式
if (convertView == null) {
convertView = LayoutInflater.from(ct).inflate(R.layout.sug_searchresult_item, null);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.search_icon);
viewHolder.tvName=(TextView)convertView.findViewById(R.id.addrname);
viewHolder.tvAddress=(TextView)convertView.findViewById(R.id.addrDetail);
convertView.setTag(viewHolder);
} else{
viewHolder = (ViewHolder) convertView.getTag();
}
//该部分主要是针对于搜索的结果,用不同的颜色标记出和关键字相同的部分。
String name=poiResult.getName();
int nBegIndex=name.indexOf(mSearchKey);
int nEndIndex=nBegIndex+mSearchKey.length();
if(nBegIndex!=-1)
{
//设置指定位置文字的颜色
SpannableStringBuilder style=new SpannableStringBuilder(name);
style.setSpan(new ForegroundColorSpan(Color.rgb(118, 173, 255)),
nBegIndex,nEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
viewHolder.tvName.setText(style);
}
//下面部分主要是对于如果搜索到的地址为空时,把搜索的名字动态居中显示。
if(poiResult.getAddress()==null||poiResult.getAddress().equals("")){
LinearLayout.LayoutParams lParams=
new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
viewHolder.tvName.setLayoutParams(lParams);
viewHolder.tvAddress.setText("");
}else{
LinearLayout.LayoutParams lParams=
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
viewHolder.tvName.setLayoutParams(lParams);
viewHolder.tvAddress.setText(poiResult.getAddress());
}
return convertView;
}
@Override
protected void onReachBottom() {
}
static class ViewHolder {
ImageView imageView;
TextView tvName;
TextView tvAddress;
}
}