效果:
使用百度导航SDK,首先需要获取的信息有:
1.起始位置的经纬度,地名
2.终点的经纬度,地名
3.起始点经纬度所使用的坐标系类型(CoordinateType )
参数如下:
private void routeplanToNavi(CoordinateType coType, double startLatitude, double startLongitude, String startName, double endLatitude, double endLongitude, String endName) {... ...}
思路:
1.起始点使用当前位置,由定位SDK获取当前位置(使用的坐标系是bd09ll);2.目的地终点,根据用户输入关键字,使用PoiNearbySearchOption结合PoiCitySearchOption转成坐标点(坐标系是bd09ll);3.将以上bd09ll坐标系转成导航2.0支持的BD09_MC,GCJ02,WGS84三者之一。
实现:
Poi搜索部分:
设置两个EditText:
textCity 目的地所属城市(为空则默认为当前城市)
textWhere 目的地
根据用户的关键字,判断进行PoiNearbySearchOption还是PoiCitySearchOption
public void startSearchPlace(String where, LatLng centerLatLng, boolean isNear) { if (where != null && where.trim().length() > 0) { if (-1 == NetworkUtil.getNetworkType(getApplicationContext())) { NetworkUtil.noNetworkHint(getApplicationContext()); } else { String textCity = etHistoryCity.getText().toString(); boolean isInputCity = textCity != null && textCity.trim().length() > 0; if (isNear) { // 周边搜索 Toast.makeText( getApplicationContext(), getResources().getString(R.string.poi_search_near) + where, Toast.LENGTH_SHORT).show(); PoiNearbySearchOption poiOption = new PoiNearbySearchOption(); poiOption.keyword(where); poiOption.location(centerLatLng); poiOption.radius(15 * 1000 * 1000); // 检索半径,单位:m poiOption.sortType(PoiSortType.distance_from_near_to_far); // 按距离排序 // poiOption.sortType(PoiSortType.comprehensive); // 按综合排序 poiOption.pageNum(0); // 分页编号 poiOption.pageCapacity(10); // 设置每页容量,默认为每页10条 try { mPoiSearch.searchNearby(poiOption); } catch (Exception e) { e.printStackTrace(); } } else { if (!isInputCity) { // 周边搜索 Toast.makeText( getApplicationContext(), getResources().getString( R.string.poi_search_near) + where, Toast.LENGTH_SHORT).show(); PoiNearbySearchOption poiOption = new PoiNearbySearchOption(); poiOption.keyword(where); poiOption.location(centerLatLng); poiOption.radius(15 * 1000 * 1000); // 检索半径,单位:m poiOption .sortType(PoiSortType.distance_from_near_to_far); // 按距离排序 // poiOption.sortType(PoiSortType.comprehensive); // // 按综合排序 poiOption.pageNum(0); // 分页编号 poiOption.pageCapacity(10); // 设置每页容量,默认为每页10条 try { mPoiSearch.searchNearby(poiOption); } catch (Exception e) { e.printStackTrace(); } } else { // 全国搜索 Toast.makeText( getApplicationContext(), getResources().getString(R.string.poi_in_city) + textCity + getResources().getString( R.string.poi_search) + where, Toast.LENGTH_SHORT).show(); PoiCitySearchOption poiOption = new PoiCitySearchOption(); poiOption.city(textCity); poiOption.keyword(where); poiOption.pageNum(0); poiOption.pageCapacity(10); mPoiSearch.searchInCity(poiOption); } // 存储搜索历史到数据库,周边不需要 int existId = naviDb.getNaviIdByKey(where); if (existId != -1) { naviDb.deleteNaviHistoryById(existId); } NaviHistory naviHistory = new NaviHistory(where, textCity); naviDb.addNaviHistory(naviHistory); naviHistoryAdapter.notifyDataSetChanged(); } } } }
坐标系转化:
/** * startLatitude,startLongitude为bd09ll * bdLocStartAfter.getLongitude()和bdLocStartAfter.getLatitude()即为转化过的坐标系 * BDLocation.BDLOCATION_BD09LL_TO_GCJ02表示从bd09ll转成gcj02坐标系 */BDLocation bdLocStartBefore = new BDLocation(); bdLocStartBefore.setLatitude(startLatitude); bdLocStartBefore.setLongitude(startLongitude); BDLocation bdLocStartAfter = LocationClient.getBDLocationInCoorType( bdLocStartBefore, BDLocation.BDLOCATION_BD09LL_TO_GCJ02);
周边和收藏
在导航的基础上,实现了周边搜索和收藏的功能:
实现方式:
1.周边使用PoiNearSearch,获取经纬度;
2.收藏使用地图选点,然后存储经纬度;
3.后面就和导航一致了。
版权声明:本文原创,转载请注明出处:http://blog.csdn.net/zhoumushui