android實(shí)現(xiàn)定位與目的地的導(dǎo)航示例代碼
今天無(wú)意中看到技術(shù)大神利用百度地圖定位并實(shí)現(xiàn)目的地導(dǎo)航的Demo。覺(jué)得很不錯(cuò),就轉(zhuǎn)載過(guò)來(lái)一起分享,下面我們看實(shí)現(xiàn)效果:
進(jìn)入后首先會(huì)得到當(dāng)前位置,在地圖上顯示出來(lái),在輸入框中輸入目的地后,就會(huì)在地圖上出現(xiàn)最佳線路,我這里設(shè)置的是距離最小的駕車線路,另外還有公交線路、步行線路,在代碼中都有詳細(xì)注釋。另外,在控制臺(tái)還輸出了線路上每一個(gè)節(jié)點(diǎn)的信息以及起始位置和目的地的距離,信息顯示的是在當(dāng)前節(jié)點(diǎn)的導(dǎo)航信息。如下圖:
接下來(lái)就看如何實(shí)現(xiàn)了,首先,注冊(cè)百度開發(fā)者賬號(hào),并進(jìn)入百度地圖API查看相關(guān)資料百度地圖API,然后就是為需要加入地圖的應(yīng)用注冊(cè)APP KEY,注冊(cè)完后,下載百度地圖jar文件,新建工程,并導(dǎo)入即可,下面看實(shí)現(xiàn)具體代碼,在代碼中有詳細(xì)注釋:
public class NavigationDemoActivity extends MapActivity {
private String mMapKey = "注冊(cè)自己的key";
private EditText destinationEditText = null;
private Button startNaviButton = null;
private MapView mapView = null;
private BMapManager mMapManager = null;
private MyLocationOverlay myLocationOverlay = null;
//onResume時(shí)注冊(cè)此listener,onPause時(shí)需要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);
//設(shè)置啟用內(nèi)置的縮放控件
mapView.setBuiltInZoomControls(true);
//設(shè)置在縮放動(dòng)畫過(guò)程中也顯示overlay,默認(rèn)為不繪制
// mapView.setDrawOverlayWhenZooming(true);
//獲取當(dāng)前位置層
myLocationOverlay = new MyLocationOverlay(this, mapView);
//將當(dāng)前位置的層添加到地圖底層中
mapView.getOverlays().add(myLocationOverlay);
// 注冊(cè)定位事件
locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
if (location != null){
//生成GEO類型坐標(biāo)并在地圖上定位到該坐標(biāo)標(biāo)示的地點(diǎn)
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();
//設(shè)置路線策略為最短距離
searchModel.setDrivingPolicy(MKSearch.ECAR_DIS_FIRST);
searchModel.init(mMapManager, new MKSearchListener() {
//獲取駕車路線回調(diào)方法
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) {
// 錯(cuò)誤號(hào)可參考MKEvent中的定義
if (error != 0 || res == null) {
Toast.makeText(NavigationDemoActivity.this, "抱歉,未找到結(jié)果", Toast.LENGTH_SHORT).show();
return;
}
RouteOverlay routeOverlay = new RouteOverlay(NavigationDemoActivity.this, mapView);
// 此處僅展示一個(gè)方案作為示例
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+"公里---節(jié)點(diǎn)數(shù)量:"+route.getNumSteps());
for (int i = 0; i < route.getNumSteps(); i++) {
MKStep step = route.getStep(i);
System.out.println("節(jié)點(diǎn)信息:"+step.getContent());
}
routeOverlay.setData(route);
mapView.getOverlays().clear();
mapView.getOverlays().add(routeOverlay);
mapView.invalidate();
mapView.getController().animateTo(res.getStart().pt);
}
//以下兩種方式和上面的駕車方案實(shí)現(xiàn)方法一樣
@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();
//設(shè)置起始地(當(dāng)前位置)
MKPlanNode startNode = new MKPlanNode();
startNode.pt = pt;
//設(shè)置目的地
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();//顯示當(dāng)前位置
myLocationOverlay.disableCompass(); // 關(guān)閉指南針
mMapManager.stop();
super.onPause();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
// 常用事件監(jiān)聽,用來(lái)處理通常的網(wǎng)絡(luò)錯(cuò)誤,授權(quán)驗(yàn)證錯(cuò)誤等
class MyGeneralListener implements MKGeneralListener {
@Override
public void onGetNetworkState(int iError) {
Log.d("MyGeneralListener", "onGetNetworkState error is "+ iError);
Toast.makeText(NavigationDemoActivity.this, "您的網(wǎng)絡(luò)出錯(cuò)啦!",
Toast.LENGTH_LONG).show();
}
@Override
public void onGetPermissionState(int iError) {
Log.d("MyGeneralListener", "onGetPermissionState error is "+ iError);
if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
// 授權(quán)Key錯(cuò)誤:
Toast.makeText(NavigationDemoActivity.this,
"請(qǐng)?jiān)贐MapApiDemoApp.java文件輸入正確的授權(quán)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>
上面就是實(shí)現(xiàn)百度地圖定位和目的地的導(dǎo)航的所有代碼啦,不知道是不是你們想要的呢?
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android基本控件ToggleButton&Switch使用指南
- Android控件之ToggleButton的使用方法
- Android之側(cè)滑菜單DrawerLayout的使用介紹
- Android中Okhttp3實(shí)現(xiàn)上傳多張圖片同時(shí)傳遞參數(shù)
- Android自定義View實(shí)現(xiàn)隨手勢(shì)滑動(dòng)控件
- Android之RecyclerView輕松實(shí)現(xiàn)下拉刷新和加載更多示例
- Android實(shí)現(xiàn)微信搖骰子游戲
- Android實(shí)現(xiàn)計(jì)步進(jìn)度的環(huán)形Progress
- Android自定義谷歌風(fēng)格ProgressBar
- Android源碼解析之屬性動(dòng)畫詳解
- Android ToggleButton 詳解及實(shí)例代碼
相關(guān)文章
基于Android實(shí)現(xiàn)可滾動(dòng)的環(huán)形菜單效果
這篇文章主要為大家詳細(xì)介紹了Android如何使用kotlin實(shí)現(xiàn)可滾動(dòng)的環(huán)形菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Android編程實(shí)現(xiàn)仿優(yōu)酷圓盤旋轉(zhuǎn)菜單效果的方法詳解【附demo源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)仿優(yōu)酷圓盤旋轉(zhuǎn)菜單效果的方法,涉及Android界面布局及事件響應(yīng)相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-08-08
flutter實(shí)現(xiàn)發(fā)送驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了flutter發(fā)送驗(yàn)證碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Android實(shí)現(xiàn)QQ登錄界面遇到問(wèn)題及解決方法
本文給大家介紹android仿qq登錄界面的實(shí)現(xiàn)代碼,在實(shí)現(xiàn)此功能過(guò)程中遇到各種問(wèn)題,但是最終都順利解決,如果大家對(duì)android qq登錄界面實(shí)現(xiàn)方法感興趣的朋友一起學(xué)習(xí)吧2016-09-09
Android實(shí)現(xiàn)網(wǎng)絡(luò)多線程斷點(diǎn)續(xù)傳下載功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)絡(luò)多線程斷點(diǎn)續(xù)傳下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
android水平循環(huán)滾動(dòng)控件使用詳解
這篇文章主要為大家詳細(xì)介紹了android水平循環(huán)滾動(dòng)控件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12

