最近闲来突然心血来潮想写一些关于地图的小dome
首先做出来的就是定位与检索。
初始化的步骤:
- (1)在application中添加开发密钥
- <application>
- <meta-data
- android:name="com.baidu.lbsapi.API_KEY"
- android:value="开发者 key" />
- </application>
- 2)添加所需权限
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
- <uses-permission android:name="android.permission.WAKE_LOCK"/>
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- <uses-permission android:name="android.permission.GET_TASKS" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.WRITE_SETTINGS" />
- 第三步,在布局xml文件中添加地图控件;
- <com.baidu.mapapi.map.MapView
- android:id="@+id/bmapView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:clickable="true" />
- 第四步,在应用程序创建时初始化 SDK引用的Context 全局变量:
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //在使用SDK各组件之前初始化context信息,传入ApplicationContext
- //注意该方法要再setContentView方法之前实现
- SDKInitializer.initialize(getApplicationContext());
- setContentView(R.layout.activity_main);
- }
第五步,创建地图Activity,管理地图生命周期;
做完这些你就可以拥有一个简单的百度地图了然后就是定位,定位这一块一定一定不要忘了在你的清单文件注册服务不然你是获取不到任何位置信息的。
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
@Override
public void onReceiveLocation(BDLocation location) {
mLocationBean = new LocationBean();
//获取定位结果
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime()); //获取定位时间
mLocationBean.setLocationTime(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType()); //获取类型类型
sb.append("\nlatitude : ");
sb.append(location.getLatitude()); //获取纬度信息
mLocationBean.setLocationLatitude(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude()); //获取经度信息
mLocationBean.setLocationLongitude(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius()); //获取定位精准度
if (location.getLocType() == BDLocation.TypeGpsLocation){
// GPS定位结果
sb.append("\nspeed : ");
sb.append(location.getSpeed()); // 单位:公里每小时
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber()); //获取卫星数
sb.append("\nheight : ");
sb.append(location.getAltitude()); //获取海拔高度信息,单位米
sb.append("\ndirection : ");
sb.append(location.getDirection()); //获取方向信息,单位度
sb.append("\naddr : ");
sb.append(location.getAddrStr()); //获取地址信息
mLocationBean.setLocationAddress(location.getAddrStr());
sb.append("\ndescribe : ");
sb.append("gps定位成功");
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
// 网络定位结果
sb.append("\naddr : ");
sb.append(location.getAddrStr()); //获取地址信息
mLocationBean.setLocationAddress(location.getAddrStr());
sb.append("\noperationers : ");
sb.append(location.getOperators()); //获取运营商信息
sb.append("\ndescribe : ");
sb.append("网络定位成功");
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {
// 离线定位结果
sb.append("\ndescribe : ");
sb.append("离线定位成功,离线定位结果也是有效的");
} else if (location.getLocType() == BDLocation.TypeServerError) {
sb.append("\ndescribe : ");
sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
sb.append("\ndescribe : ");
sb.append("网络不同导致定位失败,请检查网络是否通畅");
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
sb.append("\ndescribe : ");
sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
}
sb.append("\nlocationdescribe : ");
sb.append(location.getLocationDescribe()); //位置语义化信息
mLocationBean.setLocationDescribe(location.getLocationDescribe());
List<Poi> list = location.getPoiList(); // POI数据
if (list != null) {
sb.append("\npoilist size = : ");
sb.append(list.size());
for (Poi p : list) {
sb.append("\npoi= : ");
sb.append(p.getId() + " " + p.getName() + " " + p.getRank());
}
mLocationListener.onSuccess(mLocationBean);
}else{
mLocationListener.onFile();
}
// Log.i("BaiduLocationApiDem", sb.toString());
}
@Override
public void onConnectHotSpotMessage(String s, int i) {
}
}
OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
public void onGetPoiResult(PoiResult result) {
// 获取POI检索结果
if (result == null
|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {// 没有找到检索结果
toastShort("未找到结果");
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {// 检索结果正常返回
// baiduMap.clear();
baiduMap.clear();
// 创建PoiOverlay
PoiOverlay overlay = new MyPoiOverlay(baiduMap);
// 设置overlay可以处理标注点击事件
baiduMap.setOnMarkerClickListener(overlay);
// 设置PoiOverlay数据
overlay.setData(result);
// 添加PoiOverlay到地图中
overlay.addToMap();
overlay.zoomToSpan();
}
}
检索怎么可以不用覆盖物呢,覆盖物这块被百度开源了PoiOverlay这个类可以自己去下载,现在还很多地方能下载到我就没上传。
还有就是覆盖物的点击监听直接上代码
/**
* 覆盖物
*/
private class MyPoiOverlay extends PoiOverlay {
private PoiResult mPoiResult = null;
public MyPoiOverlay(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public void setData(PoiResult poiResult) {
// TODO Auto-generated method stub
super.setData(poiResult);
this.mPoiResult = poiResult;
}
@Override
public boolean onPoiClick(int index) {
//覆盖物点击监听,index覆盖物在地图上标识地址集合的下标
super.onPoiClick(index);
Log.e("tag", mPoiResult.getAllPoi().get(index).address);
Log.e("tag", "维度"+ mPoiResult.getAllPoi().get(index).location.latitude +"经度"+mPoiResult.getAllPoi().get(index).location.longitude);
return true;
}
}
个人认为百度开放平台写的还是很详细的。也不是什么大牛就是编码路上一直笨拙起飞的小鸟,博客可能有些措辞不太准确,对看到这篇博客的人有哪些不懂的一定详细
的说明,可以下下面留言。我的路还有好长,请大家多多包涵与指教
附上定位的一个小工具类:https://github.com/115MRLI/baidumap