百度地图api的简单应用

最近在项目中用到百度定位和百度地址搜索,特记下以备以后使用


使用的库文件是BaiduLBS_Android.jar 和libBaiduMapSDK_v3_2_0_15.so、liblocSDK5.so,网盘地址http://yun.baidu.com/share/link?shareid=1035048738&uk=2150532712&third=0可以下载

首先在androidmanifest.xml 配置百度map api相关

<!-- 百度地图配置相关 start -->
<meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="IrvpWqhB53SHyUEWI20cp0Qg" />


        <service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:process=":remote" >
            <intent-filter>
                <action android:name="com.baidu.location.service_v2.2" >
                </action>
            </intent-filter>
        </service>
<!-- 百度地图 开发相关 end -->

加权限<uses-permission android:name="android.permission.WRITE_SETTINGS" />(ps:还有啥权限就不知道了,因为项目本身已有很多权限)

在使用之前需要在MyApplication中初始化百度sdk

SDKInitializer.initialize(this);//初始化baidu SDK


百度定位并搜索当前范围内的建筑物


import java.util.ArrayList;
import java.util.List;


import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;


public class SelectLocalActivity extends BaseActivity implements OnClickListener,OnGetSuggestionResultListener
, OnGetPoiSearchResultListener{
private ListView local_list;
private TextView top_bar_left_btn;
private TextView top_title;
private ImageView top_bar_right_btn;
private SelectLocalAdapter adapter;
private List<LocalBean> dataList=new ArrayList<LocalBean>();
private Context context;
private SuggestionSearch suggestionSearch;
private LocationClient locationClient;
private LocationMode tempMode = LocationMode.Hight_Accuracy;
private SearchLocationListener locationListener;
private String tempcoor="gcj02";

private PoiSearch poiSearch;


@Override
protected void onResume() {
super.onResume();
if(locationClient != null){
locationClient.start();
}
}

@Override
public void onPause() {
if(locationClient != null){
locationClient.stop(); 
}
super.onPause();
}


@Override
protected void onDestroy() {
if(poiSearch != null){
poiSearch.destroy();
poiSearch = null;
}
super.onDestroy();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.select_local_list);
initView();
initLocation();
}
private void initView() {
context=this;
top_bar_left_btn=(TextView) findViewById(R.id.top_bar_left_btn);
top_bar_left_btn.setOnClickListener(this);
top_title=(TextView) findViewById(R.id.top_title);
top_title.setText("所在位置");
top_bar_right_btn=(ImageView) findViewById(R.id.top_bar_right_btn);
top_bar_right_btn.setVisibility(View.VISIBLE);
top_bar_right_btn.setOnClickListener(this);
top_bar_right_btn.setImageResource(R.drawable.bbs_city_search);
local_list=(ListView) findViewById(R.id.local_list);
adapter=new SelectLocalAdapter(dataList,context);
local_list.setAdapter(adapter);
adapter.setSelectedIndex(0);
local_list.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
int n=position-1;
if(n!=adapter.getSelectedIndex()){
adapter.setSelectedIndex(n);
adapter.notifyDataSetChanged();
SharedPreferencesKeeper.writeInfomation(context, 37, dataList.get(position).getName());
finish();
}

}
});
}
/**
* init location
*/
private void initLocation(){
poiSearch = PoiSearch.newInstance();
poiSearch.setOnGetPoiSearchResultListener(this);
suggestionSearch = SuggestionSearch.newInstance();
suggestionSearch.setOnGetSuggestionResultListener(this);

locationClient = new LocationClient(this);
locationListener = new SearchLocationListener();
locationClient.registerLocationListener(locationListener);

LocationClientOption option = new LocationClientOption();
option.setLocationMode(tempMode);//设置定位模式 


option.setCoorType(tempcoor);//返回的定位结果是百度经纬度,默认值gcj02
option.setIsNeedAddress(true);
locationClient.setLocOption(option); 
}
/**
* loacation listener
*/
public class SearchLocationListener implements BDLocationListener{



@Override
public void onReceiveLocation(BDLocation location) {
if (location != null) {
/**更新本地经纬度*/


//更新城市信息
String cityName = location.getCity(); 
String provinceName = location.getProvince();

SharedPreferencesKeeper.writeInfomation(context, -3, cityName);//保存城市名
SharedPreferencesKeeper.writeInfomation(context, -5, provinceName);//保存省名

PoiNearbySearchOption searchOption = new PoiNearbySearchOption();
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
searchOption.keyword(location.getDistrict()); 
searchOption.location(latLng);
searchOption.radius(1);
searchOption.pageNum(20);
poiSearch.searchNearby(searchOption);
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.top_bar_right_btn:
startActivity(new Intent(context,SearchLocaltionActivity.class));
break;
case R.id.top_bar_left_btn:
finish();
break;
default:
break;
}

}
class SelectLocalAdapter extends BaseAdapter{
private List<LocalBean> list;
private Context context;

public SelectLocalAdapter(List<LocalBean> list, Context context) {
super();
this.list = list;
this.context = context;
}


@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) {
convertView=LayoutInflater.from(context).inflate(R.layout.select_local_item, null);
holder=new ViewHolder();
holder.item_content=(TextView) convertView.findViewById(R.id.item_content);
holder.item_img=(ImageView) convertView.findViewById(R.id.item_img);
holder.item_name=(TextView) convertView.findViewById(R.id.item_name);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
LocalBean bean=list.get(position);
if (bean.getAddr().equals("")) {
holder.item_content.setVisibility(View.GONE);
}else{
holder.item_content.setVisibility(View.VISIBLE);
}
holder.item_content.setText(bean.getAddr());
holder.item_name.setText(bean.getName());
if (selectedIndex==position) {
holder.item_img.setVisibility(View.VISIBLE);
}else{
holder.item_img.setVisibility(View.GONE);
}
return convertView;
}
class ViewHolder{
TextView item_name;
TextView item_content;
ImageView item_img;
}
private int selectedIndex = 0;
public void setSelectedIndex (int index){
selectedIndex = index;
}
public int getSelectedIndex (){
return selectedIndex;
}
}
@Override
public void onGetPoiDetailResult(PoiDetailResult arg0) {
// TODO Auto-generated method stub

}


@Override
public void onGetPoiResult(PoiResult arg0) {
if(arg0 == null){
return;
}

List<PoiInfo> list = arg0.getAllPoi();

if(list != null){
LocalBean addrBean = null;
PoiInfo poiInfo = null;
int size = list.size();
for(int i = 0; i < size; i++){
poiInfo = list.get(i);
addrBean = new LocalBean();
addrBean.setName(poiInfo.city + "-" + poiInfo.name);
addrBean.setAddr(poiInfo.address);
addrBean.setLatitude(poiInfo.location.latitude);
addrBean.setLongitude(poiInfo.location.longitude);
dataList.add(addrBean);
}

if(adapter != null){
adapter.notifyDataSetChanged();
}
}

}


@Override
public void onGetSuggestionResult(SuggestionResult arg0) {
// TODO Auto-generated method stub

}

}


根据关键字搜索相关的地址


import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;


/**
 * 搜索地址
 * @author jiaqq
 *
 */
public class SearchLocaltionActivity extends BaseActivity implements OnClickListener,
OnGetSuggestionResultListener{
private TextView top_bar_left_btn;
private EditText serach_content;
private TextView search_btn;
private PullToRefreshListView local_list;
private ArrayAdapter<String> sugAdapter = null;
private SuggestionSearch mSuggestionSearch = null;
private SuggestionResult mres;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search_localtion_list);
init();
}


private void init() {
// TODO Auto-generated method stub
top_bar_left_btn=(TextView) findViewById(R.id.top_bar_left_btn);
top_bar_left_btn.setOnClickListener(this);
serach_content=(EditText) findViewById(R.id.serach_content);
search_btn=(TextView) findViewById(R.id.search_btn);
search_btn.setOnClickListener(this);
local_list=(PullToRefreshListView) findViewById(R.id.local_list);
mSuggestionSearch = SuggestionSearch.newInstance();
mSuggestionSearch.setOnGetSuggestionResultListener(this);
sugAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line);
local_list.setAdapter(sugAdapter);
local_list.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
SharedPreferencesKeeper.writeInfomation(SearchLocaltionActivity.this, 37,mres.getAllSuggestions().get(arg2-1).key );
finish();
AppManager.getAppManager().finishActivity(SelectLocalActivity.class);
}
});
}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.top_bar_left_btn:
finish();
break;
case R.id.search_btn:
String str=serach_content.getText().toString().trim();
if (str.equals("")) {
showToastMsg("请输入地址");
sugAdapter.clear();
              sugAdapter.notifyDataSetChanged();
return;
}
             mSuggestionSearch
.requestSuggestion((new SuggestionSearchOption())
.keyword(str).city("深圳"));
break;


default:
break;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mSuggestionSearch.destroy();
}
@Override
public void onGetSuggestionResult(SuggestionResult res) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (res == null || res.getAllSuggestions() == null) {
return;
}
sugAdapter.clear();
for (SuggestionResult.SuggestionInfo info : res.getAllSuggestions()) {
if (info.key != null)
sugAdapter.add(info.key);
mres=res;
}
sugAdapter.notifyDataSetChanged();
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值