android实现定位与目的地的导航示例代码

android实现定位与目的地的导航示例代码,第1张

概述今天无意中看到技术大神利用百度地图定位并实现目的地导航的Demo。觉得很不错,就转载过来一起分享,下面我们看实现效果:

今天无意中看到技术大神利用百度地图定位并实现目的地导航的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,endNode);         //公交路线 //       searchModel.transitSearch(city,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> 

上面就是实现百度地图定位和目的地的导航的所有代码啦,不知道是不是你们想要的呢?

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的android实现定位与目的地的导航示例代码全部内容,希望文章能够帮你解决android实现定位与目的地的导航示例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1146918.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存