Android 百度地圖API 定位 導航


           看看這個利用百度地圖定位並實現目的地導航的Demo。php

首先看實現效果:css

          Android 百度地圖API 定位 導航 代碼                         Android 百度地圖API 定位 導航 代碼

進 入後首先會獲得當前位置,在地圖上顯示出來。在輸入框中輸入目的地後,就會在地圖上出現最佳線路,我這裏設置的是距離最小的駕車線路,另外還有公交線路、 步行線路,在代碼中都有具體凝視。另外,在控制檯還輸出了線路上每一個節點的信息以及起始位置和目的地的距離,信息顯示的是在當前節點的導航信息。例如如下 圖:html

        Android 百度地圖API 定位 導航 代碼

 

接下來就看怎樣實現了,首先。註冊百度開發人員帳號,並進入百度地圖API查看相關資料百度地圖API,而後就是爲需要增長地圖的應用註冊APP KEY,註冊完後,下載百度地圖jar文件。新建project。並導入就能夠。如下看實現具體代碼,在代碼中有具體凝視:java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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 "您的網絡出錯啦!

"android

                         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!

"git

                             Toast.LENGTH_LONG).show(); 
                
            
        
}
而後是佈局文件:
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<? 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:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<? 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 >
相關文章
相關標籤/搜索