在Eclipse上开发百度地图的教程比较多,最近用的比较多的是Android Studio平台开发,本文主要是学习如何在Android Studio上进行百度地图的开发。
1.准备工作
这第一步网上说的很多了,就是关于百度地图的SDK下载以及Key的申请。具体教程可参考这篇博文:http://blog.csdn.net/chentravelling/article/details/50995524
2.功能实现
首先讲讲百度地图路线规划如何实现的。路线规划主要用到WalkingRouteOverlay,TransitRouteOverlay 以及 DrivingRouteOverlay这三个功能类,通过类获得起点与终点的路线信息进行规划并在地图上显示。用以下代码获得起点与终点信息,即移动端输入text文本信息:
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String start = startEt.getText().toString();
String end = endEt.getText().toString();
String city = cityEt.getText().toString();
if (city == null || city.equals("")) {
city = GuideActivity.this.city;
}
routePlan(start, end, city);
}
});
然后发起路线规划:
/**
* 发起路线规划
*/
public void routePlan(String start, String end, String city) {
mSearch = RoutePlanSearch.newInstance();
mSearch.setOnGetRoutePlanResultListener(listener);
// 起点与终点
PlanNode stNode = PlanNode.withCityNameAndPlaceName(city, start);
PlanNode enNode = PlanNode.withCityNameAndPlaceName(city, end);
// 步行路线规划
boolean res = mSearch.walkingSearch(new WalkingRoutePlanOption().from(
stNode).to(enNode));
//boolean res = mSearch.transitSearch(new TransitRoutePlanOption().from(
// stNode).city("杭州").to(enNode));
// 驾车路线规划 // mSearch.drivingSearch(new // DrivingRoutePlanOption().from(stNode).to(enNode)); }
最后获取结果:
/**
* 步行
*/
public void onGetWalkingRouteResult(WalkingRouteResult result) {
// 获取步行线路规划结果
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(GuideActivity.this, "抱歉,未找到结果",
Toast.LENGTH_SHORT).show();
}
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
// 起终点或途经点地址有岐义,通过以下接口获取建议查询信息
// result.getSuggestAddrInfo()
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
WalkingRouteOverlay overlay = new MyWalkingRouteOverlay(
mBaiduMap);
mBaiduMap.setOnMarkerClickListener(overlay);
overlay.setData(result.getRouteLines().get(0));
overlay.addToMap();
overlay.zoomToSpan();
//Toast.makeText(GuideActivity.this,"点击图标会有指示哦~",
// Toast.LENGTH_SHORT).show();
}
}
public void onGetTransitRouteResult(TransitRouteResult result) {
// 获取公交换乘路径规划结果
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(GuideActivity.this, "抱歉,未找到结果",
Toast.LENGTH_SHORT).show();
}
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
//起终点或途经点地址有岐义,通过以下接口获取建议查询信息
//result.getSuggestAddrInfo()
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
TransitRouteOverlay overlay = new MyTransitRouteOverlay(mBaiduMap);
mBaiduMap.setOnMarkerClickListener(overlay);
overlay.setData(result.getRouteLines().get(0));
overlay.addToMap();
overlay.zoomToSpan();
//Toast.makeText(GuideActivity.this,"点击图标会有指示哦~",
// Toast.LENGTH_SHORT).show();
}
}
实现效果图:
接着讲讲POI搜索功能的实现。百度地图的POI点可以分有两种:地图中已经标记的点和通过关键词搜索得到的POI点。本文主要讲讲关键词搜索的实现,百度提供了一个POI
点击事件的接口 BaiduMap.OnMapClickListener,有关模块如下:
//增加监听:长按地图
baiduMap.setOnMapLongClickListener(new OnMapLongClickListener());
//增加监听:map click listener ,主要监听poi点击
baiduMap.setOnMapClickListener(new OnMapClickListener());
//地理编码模块
mSearch = GeoCoder.newInstance();
//增加监听:地理编码查询结果
mSearch.setOnGetGeoCodeResultListener(this);
//POI搜索模块
poiSearch = PoiSearch.newInstance();
//增加监听:POI搜索结果
poiSearch.setOnGetPoiSearchResultListener(new PoiSearchListener());
//模糊搜索
suggestionSearch = SuggestionSearch.newInstance();
//增加监听:模糊搜索查询结果
suggestionSearch.setOnGetSuggestionResultListener(new SuggestionResultListener());
接口 BaiduMap.OnMapClickListener的实现:
private class OnMarkerClickListener implements BaiduMap.OnMarkerClickListener {
@Override
public boolean onMarkerClick(Marker marker) {
BDLocation bdLocation = new BDLocation();
bdLocation.setAddrStr(marker.getTitle());
bdLocation.setLatitude(marker.getPosition().latitude);
bdLocation.setLongitude(marker.getPosition().longitude);
//弹出信息
//poputInfo(bdLocation, marker.getTitle());
return false;
}
}
获得搜索结果:
@Override
public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
if (poiDetailResult == null
|| poiDetailResult.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(PoiActivity.this, "未找到结果", Toast.LENGTH_LONG)
.show();
return;
}
if (poiDetailResult.error == SearchResult.ERRORNO.NO_ERROR) {
//搜索到POI
baiduMap.clear();
baiduMap.addOverlay(
new MarkerOptions()
.position(poiDetailResult.location) //坐标位置
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding))
.title(poiDetailResult.getAddress()) //标题
);
//将该POI点设置为地图中心
baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(poiDetailResult.location));
Toast.makeText(PoiActivity.this, "搜索中...", Toast.LENGTH_LONG).show();
return;
}
if (poiDetailResult.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
}
}
@Override
public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
}
}
获得联想搜索结果:
private class SuggestionResultListener implements OnGetSuggestionResultListener {
@Override
public void onGetSuggestionResult(final SuggestionResult suggestionResult) {
if (suggestionResult == null || suggestionResult.getAllSuggestions() == null) {
return;
}
List suggest = new ArrayList<>();
for (SuggestionResult.SuggestionInfo suggestionInfo : suggestionResult.getAllSuggestions()) {
if (suggestionInfo.key != null) {
suggest.add(suggestionInfo.key);
}
}
adapter = new ArrayAdapter(PoiActivity.this, android.R.layout.simple_dropdown_item_1line, suggest);
autoCompleteTextView.setAdapter(adapter);
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
SuggestionResult.SuggestionInfo info = suggestionResult.getAllSuggestions().get(position);
poiSearch.searchPoiDetail(new PoiDetailSearchOption().poiUid(info.uid));
}
});
adapter.notifyDataSetChanged();
}
}
实现的效果如下:
开发过程中遇到的问题比较多,这里讲讲两个主要问题。
第一个问题:验证出错,错误码:230,请AndroidManifest.xml文件中检查 kye 设置。解决方法:在检查sha1值和包名,都无误,依然不能解决问题时,请:查看app
module目录下的有没有debug.keystore,如果有,用自己电脑里的debug.keystore来将其替换掉,然后rebuild,运行即可。
第二个问题:百度地图SDK v3.6.0以上版本找不到PoiOverlay类。解决方法:将官方样例代码BaiduMap_AndroidMapSDK_v4.4.0_Sample\BaiduMapsApiDemo\src下的com.baidu.mapapi
(类PoiOverlay在com.baidu.mapapi.overlayutil包中)直接复制粘贴到自己项目相应的位置中,有时需要删除clusterutil这个包。