android⾕歌定位demo,android实现定位与⽬的地的导航⽰例
代码
今天⽆意中看到技术⼤神利⽤百度地图定位并实现⽬的地导航的Demo。觉得很不错,就转载过来⼀起分享,下⾯我们看实现效果:
进⼊后⾸先会得到当前位置,在地图上显⽰出来,在输⼊框中输⼊⽬的地后,就会在地图上出现最佳线路,我这⾥设置的是距离最⼩的驾车线路,另外还有公交线路、步⾏线路,在代码中都有详细注释。另外,在控制台还输出了线路上每⼀个节点的信息以及起始位置和⽬的地的距离,信息显⽰的是在当前节点的导航信息。如下图:
接下来就看如何实现了,⾸先,注册百度开发者账号,并进⼊百度地图API查看相关资料百度地图API,然后就是为需要加⼊地图的应⽤注册APP KEY,注册完后,下载百度地图jar⽂件,新建⼯程,并导⼊即可,下⾯看实现具体代码,在代码中有详细注释:
public class NavigationDemoActivity extends MapActivity {
private String mMapKey = "注册⾃⼰的key";
private EditText destinationEditText = null;
private Button startNaviButton = null;
private MapView mapView = null;
private BMapManager mMapManager = null;
private MyLocationOverlay myLocationOverlay = null;
//onResume时注册此listener,onPause时需要Remove,注意此listener不是Android⾃带的,是百度API中的
private MKSearch searchModel;
GeoPoint pt;
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
destinationEditText = (EditText) this.findViewById(_destination); startNaviButton = (Button) this.findViewById(R.id.btn_navi);
mMapManager = new BMapManager(getApplication());
mMapManager.init(mMapKey, new MyGeneralListener());
super.initMapActivity(mMapManager);
mapView = (MapView) this.findViewById(R.id.bmapsView);
//设置启⽤内置的缩放控件
手提电脑如何无线上网mapView.setBuiltInZoomControls(true);
//设置在缩放动画过程中也显⽰overlay,默认为不绘制
// mapView.setDrawOverlayWhenZooming(true);
//获取当前位置层
myLocationOverlay = new MyLocationOverlay(this, mapView);中秋节祝福语 老师
//将当前位置的层添加到地图底层中
// 注册定位事件
locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
if (location != null){
//⽣成GEO类型坐标并在地图上定位到该坐标标⽰的地点
pt = new GeoPoint((int)(Latitude()*1e6),
(int)(Longitude()*1e6));
/
/ System.out.println("---"+Latitude() +":"+Longitude()); Controller().animateTo(pt);
}
}
//初始化搜索模块
searchModel = new MKSearch();
//设置路线策略为最短距离
searchModel.setDrivingPolicy(MKSearch.ECAR_DIS_FIRST);
searchModel.init(mMapManager, new MKSearchListener() {
//获取驾车路线回调⽅法
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) {
// 错误号可参考MKEvent中的定义
if (error != 0 || res == null) {
Toast.makeText(NavigationDemoActivity.this, "抱歉,未到结果", Toast.LENGTH_SHORT).show(); return;
}
RouteOverlay routeOverlay = new RouteOverlay(NavigationDemoActivity.this, mapView);
// 此处仅展⽰⼀个⽅案作为⽰例
MKRoute route = Plan(0).getRoute(0);
int distanceM = Distance();
String distanceKm = String.valueOf(distanceM / 1000) +"."+String.valueOf(distanceM % 1000); System.out.println("距离:"+distanceKm+"公⾥---节点数量:"+NumSteps());
for (int i = 0; i < NumSteps(); i++) {
MKStep step = Step(i);
System.out.println("节点信息:"+Content());
}
routeOverlay.setData(route);
mapView.invalidate();
}
//以下两种⽅式和上⾯的驾车⽅案实现⽅法⼀样
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult res, int error) {
//获取步⾏路线
九眼桥桥震门@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) { //获取公交线路
}
@Override
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
}
@Override
public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
}
@Override
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
}
@Override
public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
}
});防震减灾手抄报内容
startNaviButton.setOnClickListener(new OnClickListener() {
@Override
福建省中考成绩查询public void onClick(View v) {
String destination = Text().toString();
/
/设置起始地(当前位置)
MKPlanNode startNode = new MKPlanNode();
startNode.pt = pt;
//设置⽬的地
MKPlanNode endNode = new MKPlanNode();
endNode.name = destination;
//展开搜索的城市
String city = getResources().getString(R.string.beijing);
// System.out.println("----"+city+"---"+destination+"---"+pt);
searchModel.drivingSearch(city, startNode, city, endNode);
//步⾏路线
/
/ searchModel.walkingSearch(city, startNode, city, endNode);
// ansitSearch(city, startNode, endNode);
}
});
}
@Override
protected void onResume() {
mMapManager.start();
}
@Override
protected void onPause() {
myLocationOverlay.disableCompass(); // 关闭指南针
mMapManager.stop();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
房产交易税return false;
}
// 常⽤事件监听,⽤来处理通常的⽹络错误,授权验证错误等
class MyGeneralListener implements MKGeneralListener {
@Override
public void onGetNetworkState(int iError) {
Log.d("MyGeneralListener", "onGetNetworkState error is "+ iError);
Toast.makeText(NavigationDemoActivity.this, "您的⽹络出错啦!",
Toast.LENGTH_LONG).show();
}