android实现定位与目的地的导航示例代码
程序员文章站
2023-12-17 17:11:46
今天无意中看到技术大神利用百度地图定位并实现目的地导航的demo。觉得很不错,就转载过来一起分享,下面我们看实现效果:
&nbs...
今天无意中看到技术大神利用百度地图定位并实现目的地导航的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 locationlistener locationlistener; private mksearch searchmodel; geopoint pt; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.main); destinationedittext = (edittext) this.findviewbyid(r.id.et_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); //将当前位置的层添加到地图底层中 mapview.getoverlays().add(mylocationoverlay); // 注册定位事件 locationlistener = new locationlistener(){ @override public void onlocationchanged(location location) { if (location != null){ //生成geo类型坐标并在地图上定位到该坐标标示的地点 pt = new geopoint((int)(location.getlatitude()*1e6), (int)(location.getlongitude()*1e6)); // system.out.println("---"+location.getlatitude() +":"+location.getlongitude()); mapview.getcontroller().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 = res.getplan(0).getroute(0); int distancem = route.getdistance(); string distancekm = string.valueof(distancem / 1000) +"."+string.valueof(distancem % 1000); system.out.println("距离:"+distancekm+"公里---节点数量:"+route.getnumsteps()); for (int i = 0; i < route.getnumsteps(); i++) { mkstep step = route.getstep(i); system.out.println("节点信息:"+step.getcontent()); } routeoverlay.setdata(route); mapview.getoverlays().clear(); mapview.getoverlays().add(routeoverlay); mapview.invalidate(); mapview.getcontroller().animateto(res.getstart().pt); } //以下两种方式和上面的驾车方案实现方法一样 @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 = destinationedittext.gettext().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); //公交路线 // searchmodel.transitsearch(city, startnode, endnode); } }); } @override protected void onresume() { mmapmanager.getlocationmanager().requestlocationupdates(locationlistener); mylocationoverlay.enablemylocation(); mylocationoverlay.enablecompass(); // 打开指南针 mmapmanager.start(); super.onresume(); } @override protected void onpause() { mmapmanager.getlocationmanager().removeupdates(locationlistener); mylocationoverlay.disablemylocation();//显示当前位置 mylocationoverlay.disablecompass(); // 关闭指南针 mmapmanager.stop(); super.onpause(); } @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(); } @override public void ongetpermissionstate(int ierror) { log.d("mygenerallistener", "ongetpermissionstate error is "+ ierror); if (ierror == mkevent.error_permission_denied) { // 授权key错误: toast.maketext(navigationdemoactivity.this, "请在bmapapidemoapp.java文件输入正确的授权key!", toast.length_long).show(); } } } }
然后是布局文件:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="18sp" android:text="destination:" /> <edittext android:id="@+id/et_destination" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </linearlayout> <button android:id="@+id/btn_navi" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="start navigate"/> <com.baidu.mapapi.mapview android:id="@+id/bmapsview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> </linearlayout>
androidmainifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ericssonlabs" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" /> <uses-permission android:name="android.permission.access_network_state"></uses-permission> <uses-permission android:name="android.permission.access_fine_location"></uses-permission> <uses-permission android:name="android.permission.internet"></uses-permission> <uses-permission android:name="android.permission.write_external_storage"></uses-permission> <uses-permission android:name="android.permission.access_wifi_state"></uses-permission> <uses-permission android:name="android.permission.change_wifi_state"></uses-permission> <uses-permission android:name="android.permission.read_phone_state"></uses-permission> <supports-screens android:largescreens="true" android:normalscreens="true" android:smallscreens="true" android:resizeable="true" android:anydensity="true"/> <uses-sdk android:minsdkversion="3"></uses-sdk> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".navigationdemoactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
上面就是实现百度地图定位和目的地的导航的所有代码啦,不知道是不是你们想要的呢?
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。