周一又来了,我就接着上次的开发步骤(一)来吧,继续把高德地图的相关简单功能分享一下
上次写到了第六步,接着写第七步吧。
第七步:定位 + 地图选点 + 路径规划 + 实时导航
以下是我的这个功能NaviMapActivity的页面布局文件:
1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent" >
5
6
7
8 <LinearLayout
9 android:id="@+id/navibarcontainer"
10 android:layout_width="match_parent"
11 android:layout_height="wrap_content"
12 android:orientation="vertical" >
13
14
15 <LinearLayout
16 android:layout_width="match_parent"
17 android:layout_height="wrap_content"
18 android:orientation="vertical" >
19
20 <RelativeLayout
21 android:layout_width="match_parent"
22 android:layout_height="wrap_content" >
23
24 <TextView
25 android:id="@+id/navistarttext"
26 android:layout_width="wrap_content"
27 android:layout_height="wrap_content"
28 android:layout_marginLeft="@dimen/navibar_margin_left"
29 android:layout_marginRight="@dimen/margin_right"
30 android:paddingBottom="@dimen/margin_bottom"
31 android:paddingTop="@dimen/margin_top"
32 android:text="@string/navi_start"
33 android:textColor="@color/black"
34 android:textSize="@dimen/navibar_text_size" />
35
36 <AutoCompleteTextView
37 android:id="@+id/navi_start_edit"
38 android:layout_width="wrap_content"
39 android:layout_height="@dimen/navibar_edit_height"
40 android:layout_alignParentRight="true"
41 android:layout_marginRight="@dimen/navibar_margin_right"
42 android:layout_toRightOf="@id/navistarttext"
43 android:focusable="false"
44 android:hint="@string/mypoistion"
45 android:paddingBottom="@dimen/margin_bottom"
46 android:paddingTop="@dimen/margin_top"
47 android:textSize="@dimen/navibar_text_size" >
48 </AutoCompleteTextView>
49
50 <ImageView
51 android:id="@+id/navi_start_image"
52 android:layout_width="wrap_content"
53 android:layout_height="@dimen/navibar_edit_height"
54 android:layout_alignBottom="@id/navi_start_edit"
55 android:layout_alignRight="@id/navi_start_edit"
56 android:layout_alignTop="@id/navi_start_edit"
57 android:layout_marginLeft="0dp"
58 android:layout_marginRight="@dimen/margin_right"
59 android:src="@drawable/downarrow" />
60 </RelativeLayout>
61
62 <RelativeLayout
63 android:layout_width="match_parent"
64 android:layout_height="wrap_content"
65 android:orientation="horizontal" >
66
67 <TextView
68 android:id="@+id/naviendtext"
69 android:layout_width="wrap_content"
70 android:layout_height="wrap_content"
71 android:layout_marginLeft="@dimen/navibar_margin_left"
72 android:layout_marginRight="@dimen/margin_right"
73 android:text="@string/navi_end"
74 android:textColor="@color/black"
75 android:textSize="@dimen/navibar_text_size" />
76
77 <EditText
78 android:id="@+id/navi_end_edit"
79 android:layout_width="wrap_content"
80 android:layout_height="@dimen/navibar_edit_height"
81 android:layout_alignParentRight="true"
82 android:layout_marginRight="@dimen/navibar_margin_right"
83 android:layout_toRightOf="@id/naviendtext"
84 android:focusable="false"
85 android:paddingBottom="@dimen/margin_bottom"
86 android:paddingTop="@dimen/margin_top"
87 android:textSize="@dimen/navibar_text_size" >
88 </EditText>
89
90 <ImageView
91 android:id="@+id/navi_end_image"
92 android:layout_width="wrap_content"
93 android:layout_height="@dimen/navibar_edit_height"
94 android:layout_alignBottom="@id/navi_end_edit"
95 android:layout_alignRight="@id/navi_end_edit"
96 android:layout_alignTop="@id/navi_end_edit"
97 android:layout_marginRight="@dimen/margin_right"
98 android:src="@drawable/downarrow" />
99 </RelativeLayout>
100
101 <RelativeLayout
102 android:layout_width="match_parent"
103 android:layout_height="wrap_content" >
104
105 <LinearLayout
106 android:layout_width="match_parent"
107 android:layout_height="wrap_content"
108 android:gravity="center_horizontal"
109 android:orientation="horizontal" >
110
111 <Button
112 android:id="@+id/navi_route_button"
113 android:layout_width="wrap_content"
114 android:layout_height="@dimen/navibar_button_height"
115 android:layout_marginRight="@dimen/navibar_margin_right"
116 android:paddingBottom="@dimen/margin_bottom"
117 android:paddingTop="@dimen/margin_top"
118 android:text="@string/navi_route" />
119
120 <Button
121 android:id="@+id/navi_navi_button"
122 android:layout_width="wrap_content"
123 android:layout_height="@dimen/navibar_button_height"
124 android:layout_marginLeft="@dimen/navibar_margin_left"
125 android:paddingBottom="@dimen/margin_bottom"
126 android:paddingTop="@dimen/margin_top"
127 android:text="@string/navi_navi" />
128 </LinearLayout>
129 </RelativeLayout>
130 </LinearLayout>
131 </LinearLayout>
132 <!-- 引入布局文件 -->
133 <com.amap.api.maps.MapView
134 android:id="@+id/map"
135 android:layout_width="match_parent"
136 android:layout_height="wrap_content"
137 android:layout_alignParentBottom="true"
138 android:layout_below="@id/navibarcontainer" />
139 </RelativeLayout>
View Code
然后就是实现这个功能的代码:
1 package com.oysd.activity;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.app.Activity;
7 import android.app.ProgressDialog;
8 import android.content.Intent;
9 import android.content.res.Resources;
10 import android.graphics.BitmapFactory;
11 import android.location.Location;
12 import android.media.AudioManager;
13 import android.os.Bundle;
14 import android.text.InputType;
15 import android.view.View;
16 import android.view.View.OnClickListener;
17 import android.widget.AdapterView;
18 import android.widget.ArrayAdapter;
19 import android.widget.AutoCompleteTextView;
20 import android.widget.Button;
21 import android.widget.EditText;
22 import android.widget.ImageView;
23 import android.widget.TextView;
24 import android.widget.Toast;
25 import android.widget.AdapterView.OnItemClickListener;
26
27 import com.amap.api.location.AMapLocation;
28 import com.amap.api.location.AMapLocationListener;
29 import com.amap.api.location.LocationManagerProxy;
30 import com.amap.api.location.LocationProviderProxy;
31 import com.amap.api.maps.AMap;
32 import com.amap.api.maps.MapView;
33 import com.amap.api.maps.AMap.OnMapClickListener;
34 import com.amap.api.maps.model.BitmapDescriptorFactory;
35 import com.amap.api.maps.model.LatLng;
36 import com.amap.api.maps.model.Marker;
37 import com.amap.api.maps.model.MarkerOptions;
38 import com.amap.api.navi.AMapNavi;
39 import com.amap.api.navi.AMapNaviListener;
40 import com.amap.api.navi.AMapNaviViewListener;
41 import com.amap.api.navi.model.AMapNaviInfo;
42 import com.amap.api.navi.model.AMapNaviLocation;
43 import com.amap.api.navi.model.NaviInfo;
44 import com.amap.api.navi.model.NaviLatLng;
45 import com.oysd.Util.TTSController;
46 import com.oysd.ouyangmap.R;
47 import com.oysd.ouyangmap.R.drawable;
48 import com.oysd.ouyangmap.R.id;
49 import com.oysd.ouyangmap.R.layout;
50 import com.oysd.ouyangmap.R.string;
51
52 public class NaviMapActivity extends Activity implements AMapNaviViewListener, OnClickListener, OnMapClickListener {
53
54 // --------------View基本控件---------------------
55 private MapView mMapView;// 地图控件
56 private AutoCompleteTextView mStartPointText;// 起点输入
57 private EditText mEndPointText;// 终点输入
58 private Button mRouteButton;// 路径规划按钮
59 private Button mNaviButton;// 模拟导航按钮
60 private ProgressDialog mProgressDialog;// 路径规划过程显示状态
61 private ProgressDialog mGPSProgressDialog;// GPS过程显示状态
62 private ImageView mStartImage;// 起点下拉按钮
63 private ImageView mEndImage;// 终点点击按钮
64
65 // 地图和导航核心逻辑类
66 private AMap mAmap;
67 private AMapNavi mAmapNavi;
68
69 // ---------------------变量---------------------
70 private String[] mPositionMethods;// 记录起点我的位置、地图点选数组
71
72 // 驾车路径规划起点,途经点,终点的list
73 private List<NaviLatLng> mStartPoints = new ArrayList<NaviLatLng>();
74 private List<NaviLatLng> mWayPoints = new ArrayList<NaviLatLng>();
75 private List<NaviLatLng> mEndPoints = new ArrayList<NaviLatLng>();
76
77 // 记录起点、终点、途经点位置
78 private NaviLatLng mStartPoint = new NaviLatLng();
79 private NaviLatLng mEndPoint = new NaviLatLng();
80 private NaviLatLng mWayPoint = new NaviLatLng();
81
82
83 // 记录起点、终点、途经点在地图上添加的Marker
84 private Marker mStartMarker;
85 private Marker mWayMarker;
86 private Marker mEndMarker;
87 private Marker mGPSMarker;
88
89 private boolean mIsGetGPS = false;// 记录GPS定位是否成功
90 private boolean mIsStart = false;// 记录是否已我的位置发起路径规划
91
92 private ArrayAdapter<String> mPositionAdapter;
93
94 private AMapNaviListener mAmapNaviListener;
95
96 // 记录地图点击事件相应情况,根据选择不同,地图响应不同
97 private int mMapClickMode = MAP_CLICK_NO;
98 private static final int MAP_CLICK_NO = 0;// 地图不接受点击事件
99 private static final int MAP_CLICK_START = 1;// 地图点击设置起点
100 private static final int MAP_CLICK_WAY = 2;// 地图点击设置途经点
101 private static final int MAP_CLICK_END = 3;// 地图点击设置终点
102 // 记录导航种类,用于记录当前选择是驾车还是步行
103 private int mTravelMethod = WALK_NAVI_METHOD;
104 private static final int WALK_NAVI_METHOD = 1;// 步行导航
105
106
107 private int mNaviMethod;
108 private static final int NAVI_METHOD = 0;// 执行模拟导航操作
109 private static final int ROUTE_METHOD = 1;// 执行计算线路操作
110
111 private int mStartPointMethod = BY_MY_POSITION;
112 private static final int BY_MY_POSITION = 0;// 以我的位置作为起点
113 private static final int BY_MAP_POSITION = 1;// 以地图点选的点为起点
114 // 计算路的状态
115 private final static int GPSNO = 0;// 使用我的位置进行计算、GPS定位还未成功状态
116 private final static int CALCULATEERROR = 1;// 启动路径计算失败状态
117 private final static int CALCULATESUCCESS = 2;// 启动路径计算成功状态
118
119 //定位
120 private LocationManagerProxy mLocationManger;
121
122
123 private AMapLocationListener mLocationListener=new AMapLocationListener() {
124
125 @Override
126 public void onStatusChanged(String provider, int status, Bundle extras) {
127 // TODO Auto-generated method stub
128
129 }
130
131 @Override
132 public void onProviderEnabled(String provider) {
133 // TODO Auto-generated method stub
134
135 }
136
137 @Override
138 public void onProviderDisabled(String provider) {
139 // TODO Auto-generated method stub
140
141 }
142
143 @Override
144 public void onLocationChanged(Location location) {
145 // TODO Auto-generated method stub
146
147 }
148
149 @Override
150 public void onLocationChanged(AMapLocation location) {
151 if (location!=null&&location.getAMapException().getErrorCode() == 0) {
152 mIsGetGPS = true;
153 mStartPoint =new NaviLatLng(location.getLatitude(), location.getLongitude());
154
155
156 mGPSMarker.setPosition(new LatLng(
157 mStartPoint.getLatitude(), mStartPoint
158 .getLongitude()));
159 mStartPoints.clear();
160 mStartPoints.add(mStartPoint);
161
162 dissmissGPSProgressDialog();
163
164 calculateRoute();
165
166
167
168 }
169 else{
170 showToast("定位出现异常");
171 }
172 }
173 };
174
175 @Override
176 protected void onCreate(Bundle savedInstanceState) {
177 // TODO Auto-generated method stub
178 super.onCreate(savedInstanceState);
179 setContentView(R.layout.activity_navimap);
180 // 初始化所需资源、控件、事件监听
181 initResources();
182 initView(savedInstanceState);
183 initListener();
184 initMapAndNavi();
185 //MainApplication.getInstance().addActivity(this);
186 }
187
188
189 /**
190 * 初始化地图和导航相关内容
191 */
192 private void initMapAndNavi() {
193 // 初始语音播报资源
194 setVolumeControlStream(AudioManager.STREAM_MUSIC);// 设置声音控制
195 //语音播报开始
196
197 mAmapNavi = AMapNavi.getInstance(this);// 初始化导航引擎
198
199 // 初始化Marker添加到地图
200 mStartMarker = mAmap.addMarker(new MarkerOptions()
201 .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
202 .decodeResource(getResources(), R.drawable.start))));
203 mWayMarker = mAmap.addMarker(new MarkerOptions()
204 .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
205 .decodeResource(getResources(), R.drawable.way))));
206 mEndMarker = mAmap.addMarker(new MarkerOptions()
207 .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
208 .decodeResource(getResources(), R.drawable.end))));
209 mGPSMarker = mAmap.addMarker(new MarkerOptions()
210 .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
211 .decodeResource(getResources(),
212 R.drawable.location_marker))));
213 }
214 /**
215 * 初始化资源文件,主要是字符串
216 */
217 private void initResources() {
218 Resources res = getResources();
219 mPositionMethods = new String[] { res.getString(R.string.mypoistion),
220 res.getString(R.string.mappoistion) };
221
222 }
223
224 /**
225 * 初始化界面所需View控件
226 *
227 * @param savedInstanceState
228 */
229 private void initView(Bundle savedInstanceState) {
230 mMapView = (MapView) findViewById(R.id.map);
231 mMapView.onCreate(savedInstanceState);
232 mAmap=mMapView.getMap();
233 mStartPointText = (AutoCompleteTextView) findViewById(R.id.navi_start_edit);
234
235 mStartPointText
236 .setDropDownBackgroundResource(R.drawable.whitedownborder);
237 mEndPointText = (EditText) findViewById(R.id.navi_end_edit);
238 mStartPointText.setInputType(InputType.TYPE_NULL);
239 mEndPointText.setInputType(InputType.TYPE_NULL);
240
241 mPositionAdapter = new ArrayAdapter<String>(this,
242 R.layout.strategy_inputs, mPositionMethods);
243
244 mStartPointText.setAdapter(mPositionAdapter);
245
246 mRouteButton = (Button) findViewById(R.id.navi_route_button);
247 mNaviButton = (Button) findViewById(R.id.navi_navi_button);
248 mStartImage = (ImageView) findViewById(R.id.navi_start_image);
249 mEndImage = (ImageView) findViewById(R.id.navi_end_image);
250
251 TTSController ttsManager = TTSController.getInstance(this);// 初始化语音模块
252 ttsManager.init();
253 AMapNavi.getInstance(this).setAMapNaviListener(ttsManager);// 设置语音模块播报
254 }
255
256 /**
257 * 初始化所需监听
258 */
259 private void initListener() {
260 // 控件点击事件
261 mStartPointText.setOnClickListener(this);
262 mEndPointText.setOnClickListener(this);
263 mRouteButton.setOnClickListener(this);
264 mNaviButton.setOnClickListener(this);
265 mStartImage.setOnClickListener(this);
266 mEndImage.setOnClickListener(this);
267 // 设置地图点击事件
268 mAmap.setOnMapClickListener(this);
269 // 起点下拉框点击事件监听
270 mStartPointText.setOnItemClickListener(getOnItemClickListener());
271 }
272
273 /**
274 * 起点下拉框点击事件监听
275 * */
276 private OnItemClickListener getOnItemClickListener() {
277 return new OnItemClickListener() {
278 @Override
279 public void onItemClick(AdapterView<?> arg0, View arg1, int index,
280 long arg3) {
281 switch (index) {
282 // 我的位置为起点进行导航或路径规划
283 case 0:
284 mStartPointMethod = BY_MY_POSITION;
285 break;
286 // 地图点选起点进行导航或路径规划
287 case 1:
288 mStartPointMethod = BY_MAP_POSITION;
289 mMapClickMode = MAP_CLICK_START;
290 showToast("点击地图添加起点");
291 break;
292 }
293
294 }
295
296 };
297 }
298
299 // ---------------UI操作----------------
300 private void showToast(String message) {
301 Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
302 }
303
304 private void setTextDescription(TextView view, String description) {
305 view.setText(description);
306 }
307
308
309 /**
310 * 地图点击事件监听
311 * */
312 @Override
313 public void onMapClick(LatLng latLng) {
314 // 默认不接受任何操作
315 if (mMapClickMode == MAP_CLICK_NO) {
316 return;
317 }
318 // 其他情况根据起点、途经点、终点不同逻辑处理不同
319 addPointToMap(latLng);
320
321 }
322
323 /**
324 * 地图点击事件核心处理逻辑
325 * @param position
326 */
327 private void addPointToMap(LatLng position) {
328 NaviLatLng naviLatLng = new NaviLatLng(position.latitude,
329 position.longitude);
330 switch (mMapClickMode) {
331 //起点
332 case MAP_CLICK_START:
333 mStartMarker.setPosition(position);
334 mStartPoint = naviLatLng;
335 mStartPoints.clear();
336 mStartPoints.add(mStartPoint);
337 setTextDescription(mStartPointText, "已成功设置起点");
338 break;
339 //终点
340 case MAP_CLICK_END:
341 mEndMarker.setPosition(position);
342 mEndPoints.clear();
343 mEndPoint = naviLatLng;
344 mEndPoints.add(mEndPoint);
345 setTextDescription(mEndPointText, "已成功设置终点");
346 break;
347 }
348
349 }
350
351 @Override
352 public void onClick(View v) {
353 // TODO Auto-generated method stub
354 switch (v.getId()) {
355 // 路径规划按钮处理事件
356 case R.id.navi_route_button:
357 mNaviMethod = ROUTE_METHOD;
358 calculateRoute();
359 break;
360 // 模拟导航处理事件
361 case R.id.navi_navi_button:
362 mNaviMethod = NAVI_METHOD;
363 calculateRoute();
364 break;
365 // 起点点击事件
366 case R.id.navi_start_image:
367 case R.id.navi_start_edit:
368 setTextDescription(mStartPointText, null);
369 mPositionAdapter = new ArrayAdapter<String>(this,
370 R.layout.strategy_inputs, mPositionMethods);
371 mStartPointText.setAdapter(mPositionAdapter);
372 mStartPoints.clear();
373 mStartMarker.setPosition(null);
374 mStartPointText.showDropDown();
375 break;
376 // 终点点击事件
377 case R.id.navi_end_image:
378 case R.id.navi_end_edit:
379 mMapClickMode = MAP_CLICK_END;
380 mEndPoints.clear();
381 mEndMarker.setPosition(null);
382 setTextDescription(mEndPointText, "点击地图设置终点");
383 showToast("点击地图添加终点");
384 break;
385 }
386 }
387
388 @Override
389 public void onLockMap(boolean arg0) {
390 // TODO Auto-generated method stub
391
392 }
393
394 @Override
395 public void onNaviCancel() {
396 // TODO Auto-generated method stub
397
398 }
399
400 @Override
401 public void onNaviMapMode(int arg0) {
402 // TODO Auto-generated method stub
403
404 }
405
406 @Override
407 public void onNaviSetting() {
408 // TODO Auto-generated method stub
409
410 }
411
412 @Override
413 public void onNaviTurnClick() {
414 // TODO Auto-generated method stub
415
416 }
417
418 @Override
419 public void onNextRoadClick() {
420 // TODO Auto-generated method stub
421
422 }
423
424 @Override
425 public void onScanViewButtonClick() {
426 // TODO Auto-generated method stub
427
428 }
429
430 /**
431 * 算路的方法,根据选择可以进行行车和步行两种方式进行路径规划
432 */
433 private void calculateRoute() {
434 if(mStartPointMethod==BY_MY_POSITION&&!mIsGetGPS){
435 mLocationManger=LocationManagerProxy.getInstance(this);
436 //进行一次定位
437 mLocationManger.requestLocationData(LocationProviderProxy.AMapNetwork, -1, 15, mLocationListener);
438 showGPSProgressDialog();
439
440
441 return;
442
443 }
444 mIsGetGPS=false;
445 switch (mTravelMethod) {
446 // 步行导航
447 case WALK_NAVI_METHOD:
448 int walkIndex = calculateWalkRoute();
449 if (walkIndex == CALCULATEERROR) {
450 showToast("路线计算失败,检查参数情况");
451 return;
452 } else if (walkIndex == GPSNO) {
453 return;
454 }
455 break;
456 }
457 // 显示路径规划的窗体
458 showProgressDialog();
459 }
460
461
462
463 /**
464 * 导航回调函数
465 *
466 * @return
467 */
468 private AMapNaviListener getAMapNaviListener() {
469 if (mAmapNaviListener == null) {
470
471 mAmapNaviListener = new AMapNaviListener() {
472
473 @Override
474 public void onTrafficStatusUpdate() {
475 // TODO Auto-generated method stub
476
477 }
478
479 @Override
480 public void onStartNavi(int arg0) {
481 // TODO Auto-generated method stub
482
483 }
484
485 @Override
486 public void onReCalculateRouteForYaw() {
487 // TODO Auto-generated method stub
488
489 }
490
491 @Override
492 public void onReCalculateRouteForTrafficJam() {
493 // TODO Auto-generated method stub
494
495 }
496
497 @Override
498 public void onLocationChange(AMapNaviLocation location) {
499
500
501 }
502
503 @Override
504 public void onInitNaviSuccess() {
505 // TODO Auto-generated method stub
506
507 }
508
509 @Override
510 public void onInitNaviFailure() {
511 // TODO Auto-generated method stub
512
513 }
514
515 @Override
516 public void onGetNavigationText(int arg0, String arg1) {
517 // TODO Auto-generated method stub
518
519 }
520
521 @Override
522 public void onEndEmulatorNavi() {
523 // TODO Auto-generated method stub
524
525 }
526
527 @Override
528 public void onCalculateRouteSuccess() {
529 dissmissProgressDialog();
530 switch (mNaviMethod) {
531 //路径规划
532 case ROUTE_METHOD:
533 Intent intent = new Intent(NaviMapActivity.this,
534 NaviRouteActivity.class);
535 intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
536 startActivity(intent);
537
538 break;
539 //模拟导航
540 case NAVI_METHOD:
541 Intent standIntent = new Intent(NaviMapActivity.this,
542 NaviCustomActivity.class);
543 standIntent
544 .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
545 startActivity(standIntent);
546 break;
547 }
548 }
549
550 @Override
551 public void onCalculateRouteFailure(int arg0) {
552 dissmissProgressDialog();
553 showToast("路径规划出错");
554 }
555
556 @Override
557 public void onArrivedWayPoint(int arg0) {
558 // TODO Auto-generated method stub
559
560 }
561
562 @Override
563 public void onArriveDestination() {
564 // TODO Auto-generated method stub
565
566 }
567
568 @Override
569 public void onGpsOpenStatus(boolean arg0) {
570 // TODO Auto-generated method stub
571
572 }
573
574 @Override
575 public void onNaviInfoUpdated(AMapNaviInfo arg0) {
576 // TODO Auto-generated method stub
577
578 }
579
580 @Override
581 public void onNaviInfoUpdate(NaviInfo arg0) {
582
583 // TODO Auto-generated method stub
584
585 }
586 };
587 }
588 return mAmapNaviListener;
589 }
590
591
592 /**
593 * 显示GPS进度框
594 */
595 private void showGPSProgressDialog() {
596 if (mGPSProgressDialog == null)
597 mGPSProgressDialog = new ProgressDialog(this);
598 mGPSProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
599 mGPSProgressDialog.setIndeterminate(false);
600 mGPSProgressDialog.setCancelable(true);
601 mGPSProgressDialog.setMessage("定位中...");
602 mGPSProgressDialog.show();
603 }
604
605 /**
606 * 隐藏进度框
607 */
608 private void dissmissGPSProgressDialog() {
609 if (mGPSProgressDialog != null) {
610 mGPSProgressDialog.dismiss();
611 }
612 }
613
614 /**
615 * 显示进度框
616 */
617 private void showProgressDialog() {
618 if (mProgressDialog == null)
619 mProgressDialog = new ProgressDialog(this);
620 mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
621 mProgressDialog.setIndeterminate(false);
622 mProgressDialog.setCancelable(true);
623 mProgressDialog.setMessage("线路规划中");
624 mProgressDialog.show();
625 }
626
627 /**
628 * 对步行路线进行规划
629 */
630 private int calculateWalkRoute() {
631 int code = CALCULATEERROR;
632 if (mAmapNavi.calculateWalkRoute(mStartPoint, mEndPoint)) {
633 code = CALCULATESUCCESS;
634 } else {
635
636 code = CALCULATEERROR;
637 }
638
639 return code;
640 }
641
642 /**
643 * 隐藏进度框
644 */
645 private void dissmissProgressDialog() {
646 if (mProgressDialog != null) {
647 mProgressDialog.dismiss();
648 }
649 }
650
651 // -------------生命周期必须重写方法----------------
652 @Override
653 protected void onSaveInstanceState(Bundle outState) {
654 super.onSaveInstanceState(outState);
655 mMapView.onSaveInstanceState(outState);
656 }
657 @Override
658 public void onResume() {
659 super.onResume();
660 mMapView.onResume();
661 // 以上两句必须重写
662 // 以下两句逻辑是为了保证进入首页开启定位和加入导航回调
663 AMapNavi.getInstance(this).setAMapNaviListener(getAMapNaviListener());
664 mAmapNavi.startGPS();
665 TTSController.getInstance(this).startSpeaking();
666 }
667
668 @Override
669 public void onPause() {
670 super.onPause();
671 mMapView.onPause();
672 // 以上两句必须重写
673 // 下边逻辑是移除监听
674 AMapNavi.getInstance(this)
675 .removeAMapNaviListener(getAMapNaviListener());
676 }
677
678 @Override
679 public void onDestroy() {
680 super.onDestroy();
681 mMapView.onDestroy();
682
683
684
685 }
686 }
View Code
当用户选择路径规划的时候,会跳转到NaviRouteActivity,以下是其布局文件:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:background="@color/grey"
6 android:orientation="vertical" >
7
8 <LinearLayout
9 android:layout_width="match_parent"
10 android:layout_height="wrap_content"
11 android:background="@drawable/border" >
12
13 <ImageView
14 android:id="@+id/route_back_view"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:layout_marginBottom="@dimen/margin_bottom"
18 android:layout_marginTop="@dimen/margin_top"
19 android:src="@drawable/back_top" />
20
21 <TextView
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:layout_marginBottom="@dimen/margin_bottom"
25 android:layout_marginTop="@dimen/margin_top"
26 android:layout_weight="1"
27 android:gravity="center_horizontal"
28 android:text="@string/routeshow"
29 android:textColor="@color/black"
30 android:textSize="@dimen/titletext_size" />
31 </LinearLayout>
32
33 <LinearLayout
34 android:layout_width="match_parent"
35 android:layout_height="wrap_content"
36 android:layout_weight="1"
37 android:background="@drawable/border" >
38
39 <com.amap.api.maps.MapView
40 android:id="@+id/routemap"
41 android:layout_width="match_parent"
42 android:layout_height="match_parent" />
43 </LinearLayout>
44
45 <LinearLayout
46 android:layout_width="match_parent"
47 android:layout_height="wrap_content" >
48
49 <TextView
50 android:layout_width="wrap_content"
51 android:layout_height="wrap_content"
52 android:layout_gravity="center_horizontal"
53 android:paddingBottom="@dimen/margin_bottom"
54 android:paddingLeft="@dimen/margin_left"
55 android:paddingTop="@dimen/margin_top"
56 android:text="@string/routeinfo_1"
57 android:textColor="@color/black"
58 android:textSize="@dimen/route_text_size" />
59
60 <TextView
61 android:id="@+id/navi_route_distance"
62 android:layout_width="wrap_content"
63 android:layout_height="wrap_content"
64 android:layout_gravity="center_horizontal"
65 android:paddingBottom="@dimen/margin_bottom"
66 android:paddingLeft="@dimen/margin_left"
67 android:paddingTop="@dimen/margin_top"
68 android:text="@string/routeinfo_default"
69 android:textColor="@color/red"
70 android:textSize="@dimen/route_text_size" />
71
72 <TextView
73 android:layout_width="wrap_content"
74 android:layout_height="wrap_content"
75 android:layout_gravity="center_horizontal"
76 android:paddingBottom="@dimen/margin_bottom"
77 android:paddingTop="@dimen/margin_top"
78 android:text="@string/routeinfo_2"
79 android:textColor="@color/black"
80 android:textSize="@dimen/route_text_size" />
81
82 <TextView
83 android:id="@+id/navi_route_time"
84 android:layout_width="wrap_content"
85 android:layout_height="wrap_content"
86 android:layout_gravity="center_horizontal"
87 android:paddingBottom="@dimen/margin_bottom"
88 android:paddingLeft="@dimen/margin_left"
89 android:paddingTop="@dimen/margin_top"
90 android:text="@string/routeinfo_default"
91 android:textColor="@color/red"
92 android:textSize="@dimen/route_text_size" />
93
94 <TextView
95 android:id="@+id/navi_route_cost_before"
96 android:layout_width="wrap_content"
97 android:layout_height="wrap_content"
98 android:layout_gravity="center_horizontal"
99 android:paddingBottom="@dimen/margin_bottom"
100 android:paddingTop="@dimen/margin_top"
101 android:text="@string/routeinfo_3"
102 android:textColor="@color/black"
103 android:textSize="@dimen/route_text_size" />
104
105 <TextView
106 android:id="@+id/navi_route_cost"
107 android:layout_width="wrap_content"
108 android:layout_height="wrap_content"
109 android:layout_gravity="center_horizontal"
110 android:paddingBottom="@dimen/margin_bottom"
111 android:paddingLeft="@dimen/margin_left"
112 android:paddingTop="@dimen/margin_top"
113 android:text="@string/routeinfo_default"
114 android:textColor="@color/red"
115 android:singleLine="true"
116 android:textSize="@dimen/route_text_size" />
117
118 <TextView
119 android:id="@+id/navi_route_cost_after"
120 android:layout_width="wrap_content"
121 android:layout_height="wrap_content"
122 android:layout_gravity="center_horizontal"
123 android:paddingBottom="@dimen/margin_bottom"
124 android:paddingTop="@dimen/margin_top"
125 android:text="@string/routeinfo_4"
126 android:textColor="@color/black"
127 android:textSize="@dimen/route_text_size" />
128 </LinearLayout>
129
130 <RelativeLayout
131 android:layout_width="match_parent"
132 android:layout_height="wrap_content" >
133
134 <Button
135 android:id="@+id/routestartnavi"
136 android:layout_width="wrap_content"
137 android:layout_height="@dimen/naviroute_navi_height"
138 android:layout_alignParentRight="true"
139 android:text="@string/start_navi_navi" />
140
141 <AutoCompleteTextView
142 android:id="@+id/navi_theme_text"
143 android:layout_width="wrap_content"
144 android:layout_height="@dimen/naviroute_navi_height"
145 android:layout_alignParentLeft="true"
146 android:layout_toLeftOf="@id/routestartnavi"
147 android:focusable="false"
148 android:hint="@string/theme_blue" >
149 </AutoCompleteTextView>
150
151 <ImageView
152 android:id="@+id/navi_theme_image"
153 android:layout_width="wrap_content"
154 android:layout_height="@dimen/naviroute_navi_height"
155 android:layout_alignBottom="@+id/navi_theme_text"
156 android:layout_alignRight="@+id/navi_theme_text"
157 android:layout_alignTop="@id/navi_theme_text"
158 android:layout_marginRight="@dimen/margin_right"
159 android:src="@drawable/downarrow" />
160 </RelativeLayout>
161
162 </LinearLayout>
View Code
紧接着是其实现代码(主要来自高德API官网):
1 package com.oysd.activity;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.content.res.Resources;
6 import android.os.Bundle;
7 import android.text.InputType;
8 import android.view.KeyEvent;
9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.ArrayAdapter;
12 import android.widget.AutoCompleteTextView;
13 import android.widget.Button;
14 import android.widget.ImageView;
15 import android.widget.TextView;
16
17 import com.amap.api.maps.AMap;
18 import com.amap.api.maps.MapView;
19 import com.amap.api.maps.AMap.OnMapLoadedListener;
20 import com.amap.api.navi.AMapNavi;
21 import com.amap.api.navi.AMapNaviViewOptions;
22 import com.amap.api.navi.model.AMapNaviPath;
23 import com.amap.api.navi.view.RouteOverLay;
24 import com.oysd.ouyangmap.R;
25 import com.oysd.ouyangmap.R.drawable;
26 import com.oysd.ouyangmap.R.id;
27 import com.oysd.ouyangmap.R.layout;
28 import com.oysd.ouyangmap.R.string;
29
30 /**
31 * 路径规划结果展示界面
32 */
33 public class NaviRouteActivity extends Activity implements OnClickListener,
34 OnMapLoadedListener {
35
36 // View
37 private Button mStartNaviButton;// 实时导航按钮
38 private MapView mMapView;// 地图控件
39 private AutoCompleteTextView mThemeText;// 选择导航界面的风格
40 private ImageView mThemeImage;// 选择按钮
41 private ImageView mRouteBackView;// 返回按钮
42 private TextView mRouteDistanceView;// 距离显示控件
43 private TextView mRouteTimeView;// 时间显示控件
44 private TextView mRouteCostView;// 花费显示控件
45 // 地图导航资源
46 private AMap mAmap;
47 private AMapNavi mAmapNavi;
48 private RouteOverLay mRouteOverLay;
49 // 主题数组
50 private String mTheme[];
51
52 private boolean mIsMapLoaded = false;
53
54 protected void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
56 setContentView(R.layout.activity_route);
57 initResources();
58 initView(savedInstanceState);
59 //MainApplication.getInstance().addActivity(this);
60 }
61
62 // -----------------------初始化----------------------------------
63
64 /**
65 * 初始化资源
66 */
67 private void initResources() {
68 Resources res = getResources();
69 mTheme = new String[] { res.getString(R.string.theme_blue),
70 res.getString(R.string.theme_pink),
71 res.getString(R.string.theme_white) };
72 }
73
74 /**
75 * 初始化控件
76 */
77 private void initView(Bundle savedInstanceState) {
78 mStartNaviButton = (Button) findViewById(R.id.routestartnavi);
79
80 mRouteBackView = (ImageView) findViewById(R.id.route_back_view);
81
82 mThemeText = (AutoCompleteTextView) findViewById(R.id.navi_theme_text);
83 mThemeText.setInputType(InputType.TYPE_NULL);
84 ArrayAdapter<String> themeAdapter = new ArrayAdapter<String>(this,
85 R.layout.strategy_inputs, mTheme);
86 mThemeText.setAdapter(themeAdapter);
87
88 mThemeText.setDropDownBackgroundResource(R.drawable.whitedownborder);
89
90 mThemeImage = (ImageView) findViewById(R.id.navi_theme_image);
91 mRouteDistanceView = (TextView) findViewById(R.id.navi_route_distance);
92 mRouteTimeView = (TextView) findViewById(R.id.navi_route_time);
93 mRouteCostView = (TextView) findViewById(R.id.navi_route_cost);
94 mMapView = (MapView) findViewById(R.id.routemap);
95 mMapView.onCreate(savedInstanceState);
96 mAmap = mMapView.getMap();
97 mAmap.setOnMapLoadedListener(this);
98 mThemeImage.setOnClickListener(this);
99 mThemeText.setOnClickListener(this);
100 mStartNaviButton.setOnClickListener(this);
101 mRouteBackView.setOnClickListener(this);
102 mRouteOverLay = new RouteOverLay(mAmap, null);
103 }
104
105 /**
106 * 初始化路线描述信息和加载线路
107 */
108 private void initNavi() {
109
110 mAmapNavi = AMapNavi.getInstance(this);
111 AMapNaviPath naviPath = mAmapNavi.getNaviPath();
112 if (naviPath == null) {
113 return;
114 }
115 // 获取路径规划线路,显示到地图上
116 mRouteOverLay.setRouteInfo(naviPath);
117 mRouteOverLay.addToMap();
118 if (mIsMapLoaded) {
119 mRouteOverLay.zoomToSpan();
120 }
121
122 double length = ((int) (naviPath.getAllLength() / (double) 1000 * 10))
123 / (double) 10;
124 // 不足分钟 按分钟计
125 int time = (naviPath.getAllTime() + 59) / 60;
126 int cost = naviPath.getTollCost();
127 mRouteDistanceView.setText(String.valueOf(length));
128 mRouteTimeView.setText(String.valueOf(time));
129 mRouteCostView.setText(String.valueOf(cost));
130 }
131
132 /**
133 * 获取导航界面主题样式
134 *
135 * @param themeColor
136 * @return
137 */
138 private int getThemeStyle(String themeColor) {
139 int theme = AMapNaviViewOptions.BLUE_COLOR_TOPIC;
140 if (mTheme[0].equals(themeColor)) {
141 theme = AMapNaviViewOptions.BLUE_COLOR_TOPIC;
142 } else if (mTheme[1].equals(themeColor)) {
143 theme = AMapNaviViewOptions.PINK_COLOR_TOPIC;
144 } else if (mTheme[2].equals(themeColor)) {
145 theme = AMapNaviViewOptions.WHITE_COLOR_TOPIC;
146 }
147 return theme;
148 }
149
150 // ------------------------------事件处理-----------------------------
151 @Override
152 public void onClick(View v) {
153 switch (v.getId()) {
154 // 实时导航操作
155 case R.id.routestartnavi:
156 Bundle bundle = new Bundle();
157 bundle.putInt("theme", getThemeStyle(mThemeText.getText()
158 .toString()));
159 Intent routeIntent = new Intent(NaviRouteActivity.this,
160 NaviCustomActivity.class);
161 routeIntent.putExtras(bundle);
162 startActivity(routeIntent);
163 break;
164 // 返回操作
165 case R.id.route_back_view:
166 Intent startIntent = new Intent(NaviRouteActivity.this,
167 NaviMapActivity.class);
168 startIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
169 startActivity(startIntent);
170 //MainApplication.getInstance().deleteActivity(this);
171 finish();
172 break;
173 // 主题选择
174 case R.id.navi_theme_image:
175 case R.id.navi_theme_text:
176 mThemeText.showDropDown();
177 break;
178 }
179
180 }
181
182 /**
183 *
184 * 返回键监听
185 * */
186 @Override
187 public boolean onKeyDown(int keyCode, KeyEvent event) {
188
189 if (keyCode == KeyEvent.KEYCODE_BACK) {
190 Intent intent = new Intent(NaviRouteActivity.this,
191 NaviMapActivity.class);
192 intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
193 startActivity(intent);
194 //MainApplication.getInstance().deleteActivity(this);
195 finish();
196
197 }
198 return super.onKeyDown(keyCode, event);
199 }
200
201 // ------------------------------生命周期必须重写方法---------------------------
202 @Override
203 protected void onSaveInstanceState(Bundle outState) {
204 super.onSaveInstanceState(outState);
205 mMapView.onSaveInstanceState(outState);
206 }
207
208 @Override
209 public void onResume() {
210 super.onResume();
211 mMapView.onResume();
212 initNavi();
213 }
214
215 @Override
216 public void onPause() {
217 super.onPause();
218 mMapView.onPause();
219 }
220
221 @Override
222 public void onDestroy() {
223 super.onDestroy();
224 mMapView.onDestroy();
225 }
226
227 @Override
228 public void onMapLoaded() {
229 mIsMapLoaded = true;
230 if (mRouteOverLay != null) {
231 mRouteOverLay.zoomToSpan();
232
233 }
234 }
235
236 }
View Code
当用户点击模拟导航 或者 这路径规划界面点击实时导航的时候,界面跳转到NaviCustomActivity,以下是其布局文件:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@+id/map_container"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:orientation="vertical" >
7 <com.amap.api.navi.AMapNaviView
8 android:id="@+id/customnavimap"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent"
11 />
12
13 </LinearLayout>
View Code
然后是其实现代码:
1 package com.oysd.activity;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.view.KeyEvent;
7
8 import com.amap.api.navi.AMapNavi;
9 import com.amap.api.navi.AMapNaviListener;
10 import com.amap.api.navi.AMapNaviView;
11 import com.amap.api.navi.AMapNaviViewListener;
12 import com.amap.api.navi.AMapNaviViewOptions;
13 import com.amap.api.navi.model.AMapNaviInfo;
14 import com.amap.api.navi.model.AMapNaviLocation;
15 import com.amap.api.navi.model.NaviInfo;
16 import com.oysd.Util.TTSController;
17 import com.oysd.Util.Utils;
18 import com.oysd.ouyangmap.R;
19 import com.oysd.ouyangmap.R.id;
20 import com.oysd.ouyangmap.R.layout;
21
22 /**
23 * 实时导航界面
24 *
25 */
26 public class NaviCustomActivity extends Activity implements
27 AMapNaviViewListener {
28
29 private AMapNaviView mAmapAMapNaviView;
30 // 导航可以设置的参数
31 private boolean mDayNightFlag = Utils.DAY_MODE;// 默认为白天模式
32 private boolean mDeviationFlag = Utils.YES_MODE;// 默认进行偏航重算
33 private boolean mJamFlag = Utils.YES_MODE;// 默认进行拥堵重算
34 private boolean mTrafficFlag = Utils.OPEN_MODE;// 默认进行交通播报
35 private boolean mCameraFlag = Utils.OPEN_MODE;// 默认进行摄像头播报
36 private boolean mScreenFlag = Utils.YES_MODE;// 默认是屏幕常亮
37 // 导航界面风格
38 private int mThemeStle;
39 // 导航监听
40 private AMapNaviListener mAmapNaviListener;
41
42 public void onCreate(Bundle savedInstanceState) {
43 super.onCreate(savedInstanceState);
44 setContentView(R.layout.activity_navicustom);
45 //语音播报开始
46 TTSController.getInstance(this).startSpeaking();
47 // 实时导航方式进行导航
48 AMapNavi.getInstance(this).startNavi(AMapNavi.GPSNaviMode);
49
50 initView(savedInstanceState);
51 }
52
53 private void initView(Bundle savedInstanceState) {
54 mAmapAMapNaviView = (AMapNaviView) findViewById(R.id.customnavimap);
55 mAmapAMapNaviView.onCreate(savedInstanceState);
56 mAmapAMapNaviView.setAMapNaviViewListener(this);
57 setAmapNaviViewOptions();
58 }
59
60 /**
61 * 设置导航的参数
62 */
63 private void setAmapNaviViewOptions() {
64 if (mAmapAMapNaviView == null) {
65 return;
66 }
67 AMapNaviViewOptions viewOptions = new AMapNaviViewOptions();
68 viewOptions.setSettingMenuEnabled(true);// 设置导航setting可用
69 viewOptions.setNaviNight(mDayNightFlag);// 设置导航是否为黑夜模式
70 viewOptions.setReCalculateRouteForYaw(mDeviationFlag);// 设置导偏航是否重算
71 viewOptions.setReCalculateRouteForTrafficJam(mJamFlag);// 设置交通拥挤是否重算
72 viewOptions.setTrafficInfoUpdateEnabled(mTrafficFlag);// 设置是否更新路况
73 viewOptions.setCameraInfoUpdateEnabled(mCameraFlag);// 设置摄像头播报
74 viewOptions.setScreenAlwaysBright(mScreenFlag);// 设置屏幕常亮情况
75 viewOptions.setNaviViewTopic(mThemeStle);// 设置导航界面主题样式
76
77 mAmapAMapNaviView.setViewOptions(viewOptions);
78
79 }
80
81 private AMapNaviListener getAMapNaviListener() {
82 if (mAmapNaviListener == null) {
83
84 mAmapNaviListener = new AMapNaviListener() {
85
86 @Override
87 public void onTrafficStatusUpdate() {
88 // TODO Auto-generated method stub
89
90 }
91
92 @Override
93 public void onStartNavi(int arg0) {
94 // TODO Auto-generated method stub
95
96 }
97
98 @Override
99 public void onReCalculateRouteForYaw() {
100 // 可以在频繁重算时进行设置,例如五次之后
101 // i++;
102 // if (i >= 5) {
103 // AMapNaviViewOptions viewOptions = new
104 // AMapNaviViewOptions();
105 // viewOptions.setReCalculateRouteForYaw(false);
106 // mAmapAMapNaviView.setViewOptions(viewOptions);
107 // }
108 }
109
110 @Override
111 public void onReCalculateRouteForTrafficJam() {
112
113 }
114
115 @Override
116 public void onLocationChange(AMapNaviLocation location) {
117
118 }
119
120 @Override
121 public void onInitNaviSuccess() {
122 // TODO Auto-generated method stub
123
124 }
125
126 @Override
127 public void onInitNaviFailure() {
128 // TODO Auto-generated method stub
129
130 }
131
132 @Override
133 public void onGetNavigationText(int arg0, String arg1) {
134 // TODO Auto-generated method stub
135
136 }
137
138 @Override
139 public void onEndEmulatorNavi() {
140 // TODO Auto-generated method stub
141
142 }
143
144 @Override
145 public void onCalculateRouteSuccess() {
146
147 }
148
149 @Override
150 public void onCalculateRouteFailure(int arg0) {
151
152 }
153
154 @Override
155 public void onArrivedWayPoint(int arg0) {
156 // TODO Auto-generated method stub
157
158 }
159
160 @Override
161 public void onArriveDestination() {
162 // TODO Auto-generated method stub
163
164 }
165
166 @Override
167 public void onGpsOpenStatus(boolean arg0) {
168 // TODO Auto-generated method stub
169
170 }
171
172 @Override
173 public void onNaviInfoUpdated(AMapNaviInfo arg0) {
174 // TODO Auto-generated method stub
175
176 }
177
178 @Override
179 public void onNaviInfoUpdate(NaviInfo arg0) {
180
181 // TODO Auto-generated method stub
182
183 }
184 };
185 }
186 return mAmapNaviListener;
187 }
188
189 // -------处理
190 /**
191 * 导航界面返回按钮监听
192 * */
193 @Override
194 public void onNaviCancel() {
195 Intent intent = new Intent(NaviCustomActivity.this,
196 FunctionsListActivity.class);
197 intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
198 startActivity(intent);
199 finish();
200 }
201
202 /**
203 * 点击设置按钮的事件
204 */
205 @Override
206 public void onNaviSetting() {
207 Bundle bundle = new Bundle();
208 bundle.putInt(Utils.THEME, mThemeStle);
209 bundle.putBoolean(Utils.DAY_NIGHT_MODE, mDayNightFlag);
210 bundle.putBoolean(Utils.DEVIATION, mDeviationFlag);
211 bundle.putBoolean(Utils.JAM, mJamFlag);
212 bundle.putBoolean(Utils.TRAFFIC, mTrafficFlag);
213 bundle.putBoolean(Utils.CAMERA, mCameraFlag);
214 bundle.putBoolean(Utils.SCREEN, mScreenFlag);
215 Intent intent = new Intent(NaviCustomActivity.this,
216 NaviSettingActivity.class);
217 intent.putExtras(bundle);
218 startActivity(intent);
219
220 }
221
222 @Override
223 public void onNaviMapMode(int arg0) {
224
225 }
226 @Override
227 public void onNaviTurnClick() {
228
229
230 }
231
232 @Override
233 public void onNextRoadClick() {
234 // TODO Auto-generated method stub
235
236 }
237
238 @Override
239 public void onScanViewButtonClick() {
240 // TODO Auto-generated method stub
241
242 }
243 private void processBundle(Bundle bundle) {
244
245 if (bundle != null) {
246 mDayNightFlag = bundle.getBoolean(Utils.DAY_NIGHT_MODE,
247 mDayNightFlag);
248 mDeviationFlag = bundle.getBoolean(Utils.DEVIATION, mDeviationFlag);
249 mJamFlag = bundle.getBoolean(Utils.JAM, mJamFlag);
250 mTrafficFlag = bundle.getBoolean(Utils.TRAFFIC, mTrafficFlag);
251 mCameraFlag = bundle.getBoolean(Utils.CAMERA, mCameraFlag);
252 mScreenFlag = bundle.getBoolean(Utils.SCREEN, mScreenFlag);
253 mThemeStle = bundle.getInt(Utils.THEME);
254
255 }
256 }
257
258 @Override
259 protected void onNewIntent(Intent intent) {
260 // TODO Auto-generated method stub
261 super.onNewIntent(intent);
262 setIntent(intent);
263 }
264
265 @Override
266 public boolean onKeyDown(int keyCode, KeyEvent event) {
267
268 if (keyCode == KeyEvent.KEYCODE_BACK) {
269 Intent intent = new Intent(NaviCustomActivity.this,
270 NaviMapActivity.class);
271 intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
272 startActivity(intent);
273 finish();
274 }
275 return super.onKeyDown(keyCode, event);
276 }
277
278 // ------------------------------生命周期方法---------------------------
279 @Override
280 protected void onSaveInstanceState(Bundle outState) {
281 super.onSaveInstanceState(outState);
282 mAmapAMapNaviView.onSaveInstanceState(outState);
283 }
284
285 @Override
286 public void onResume() {
287 // TODO Auto-generated method stub
288 super.onResume();
289 Bundle bundle = getIntent().getExtras();
290 processBundle(bundle);
291 setAmapNaviViewOptions();
292 AMapNavi.getInstance(this).setAMapNaviListener(getAMapNaviListener());
293 mAmapAMapNaviView.onResume();
294
295 }
296
297 @Override
298 public void onPause() {
299 mAmapAMapNaviView.onPause();
300 super.onPause();
301 AMapNavi.getInstance(this)
302 .removeAMapNaviListener(getAMapNaviListener());
303
304 }
305
306 @Override
307 public void onDestroy() {
308
309 super.onDestroy();
310 mAmapAMapNaviView.onDestroy();
311 //页面结束时,停止语音播报
312 TTSController.getInstance(this).stopSpeaking();
313 }
314
315 @Override
316 public void onLockMap(boolean arg0) {
317
318 // TODO Auto-generated method stub
319
320 }
321
322
323
324 }
View Code
其中包括页面的设置界面,NaviSettingActivity的布局文件:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:background="@color/grey"
6 android:orientation="vertical" >
7
8 <RelativeLayout
9 android:layout_width="match_parent"
10 android:layout_height="wrap_content"
11 android:layout_marginBottom="@dimen/setting_margin_bottom" >
12
13 <ImageView
14 android:id="@+id/setting_back_image"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:src="@drawable/back_top" />
18
19 <TextView
20 android:layout_width="wrap_content"
21 android:layout_height="wrap_content"
22 android:layout_centerHorizontal="true"
23 android:layout_weight="1"
24 android:gravity="center_horizontal"
25 android:text="@string/setting"
26 android:textColor="@color/black"
27 android:textSize="@dimen/titletext_size" />
28 </RelativeLayout>
29
30 <RelativeLayout
31 android:layout_width="match_parent"
32 android:layout_height="wrap_content"
33 android:layout_marginBottom="@dimen/setting_margin_bottom"
34 android:layout_marginLeft="@dimen/navibar_margin_left"
35 android:layout_marginRight="@dimen/navibar_margin_right"
36 android:layout_marginTop="@dimen/setting_margin_top"
37 android:layout_weight="1"
38 android:gravity="center_vertical"
39 android:background="@drawable/whiteborder" >
40
41 <TextView
42 android:layout_width="wrap_content"
43 android:layout_height="wrap_content"
44 android:layout_marginLeft="@dimen/margin_left"
45 android:layout_marginTop="10dp"
46 android:gravity="center_vertical"
47 android:text="@string/daynightmode"
48 android:textColor="@color/black"
49 android:textSize="@dimen/navisetting_subtext_size" />
50
51 <RadioGroup
52 android:id="@+id/day_night_group"
53 android:layout_width="wrap_content"
54 android:layout_height="wrap_content"
55 android:layout_alignParentRight="true"
56 android:layout_marginRight="@dimen/margin_right"
57 android:orientation="horizontal" >
58
59 <RadioButton
60 android:id="@+id/dayratio"
61 android:layout_width="wrap_content"
62 android:layout_height="wrap_content"
63 android:layout_marginRight="@dimen/margin_right"
64 android:checked="true"
65 android:text="@string/day"
66 android:textColor="@color/black" />
67
68 <RadioButton
69 android:id="@+id/nightradio"
70 android:layout_width="wrap_content"
71 android:layout_height="wrap_content"
72 android:text="@string/night"
73 android:textColor="@color/black" />
74 </RadioGroup>
75 </RelativeLayout>
76
77 <RelativeLayout
78 android:layout_weight="1"
79 android:layout_width="match_parent"
80 android:layout_height="wrap_content"
81 android:layout_marginBottom="@dimen/setting_margin_bottom"
82 android:layout_marginLeft="@dimen/navibar_margin_left"
83 android:layout_marginRight="@dimen/navibar_margin_right"
84 android:layout_marginTop="@dimen/setting_margin_top"
85 android:gravity="center_vertical"
86 android:background="@drawable/whiteborder" >
87
88 <TextView
89 android:layout_width="wrap_content"
90 android:layout_height="wrap_content"
91 android:layout_marginLeft="@dimen/margin_left"
92 android:layout_marginTop="10dp"
93 android:text="@string/deviationrecalculation"
94 android:textColor="@color/black"
95 android:textSize="@dimen/navisetting_subtext_size" />
96
97 <RadioGroup
98 android:id="@+id/deviation_group"
99 android:layout_width="wrap_content"
100 android:layout_height="wrap_content"
101 android:layout_alignParentRight="true"
102 android:layout_marginRight="@dimen/margin_right"
103 android:orientation="horizontal" >
104
105 <RadioButton
106 android:id="@+id/deviationyesradio"
107 android:layout_width="wrap_content"
108 android:layout_height="wrap_content"
109 android:layout_marginRight="@dimen/margin_right"
110 android:checked="true"
111 android:text="@string/yes"
112 android:textColor="@color/black" />
113
114 <RadioButton
115 android:id="@+id/deviationnoradio"
116 android:layout_width="wrap_content"
117 android:layout_height="wrap_content"
118 android:text="@string/no"
119 android:textColor="@color/black" />
120 </RadioGroup>
121 </RelativeLayout>
122
123 <RelativeLayout
124 android:layout_weight="1"
125 android:layout_width="match_parent"
126 android:layout_height="wrap_content"
127 android:gravity="center_vertical"
128 android:layout_marginBottom="@dimen/setting_margin_bottom"
129 android:layout_marginLeft="@dimen/navibar_margin_left"
130 android:layout_marginRight="@dimen/navibar_margin_right"
131 android:layout_marginTop="@dimen/setting_margin_top"
132 android:background="@drawable/whiteborder" >
133
134 <TextView
135 android:layout_width="wrap_content"
136 android:layout_height="wrap_content"
137 android:layout_marginLeft="@dimen/margin_left"
138 android:layout_marginTop="10dp"
139 android:text="@string/jamrecalculation"
140 android:textColor="@color/black"
141 android:textSize="@dimen/navisetting_subtext_size" />
142
143 <RadioGroup
144 android:id="@+id/jam_group"
145 android:layout_width="wrap_content"
146 android:layout_height="wrap_content"
147 android:layout_alignParentRight="true"
148 android:layout_marginRight="@dimen/margin_right"
149 android:orientation="horizontal" >
150
151 <RadioButton
152 android:id="@+id/jam_yes_radio"
153 android:layout_width="wrap_content"
154 android:layout_height="wrap_content"
155 android:layout_marginRight="@dimen/margin_right"
156 android:checked="true"
157 android:text="@string/yes"
158 android:textColor="@color/black" />
159
160 <RadioButton
161 android:id="@+id/jam_no_radio"
162 android:layout_width="wrap_content"
163 android:layout_height="wrap_content"
164 android:text="@string/no"
165 android:textColor="@color/black" />
166 </RadioGroup>
167 </RelativeLayout>
168
169 <RelativeLayout
170 android:layout_weight="1"
171 android:layout_width="match_parent"
172 android:layout_height="wrap_content"
173 android:gravity="center_vertical"
174 android:layout_marginBottom="@dimen/setting_margin_bottom"
175 android:layout_marginLeft="@dimen/navibar_margin_left"
176 android:layout_marginRight="@dimen/navibar_margin_right"
177 android:layout_marginTop="@dimen/setting_margin_top"
178 android:background="@drawable/whiteborder" >
179
180 <TextView
181 android:id="@+id/textView2"
182 android:layout_width="wrap_content"
183 android:layout_height="wrap_content"
184 android:layout_marginLeft="@dimen/margin_left"
185 android:layout_marginTop="10dp"
186 android:text="@string/trafficbroadcast"
187 android:textColor="@color/black"
188 android:textSize="@dimen/navisetting_subtext_size" />
189
190 <RadioGroup
191 android:id="@+id/traffic_group"
192 android:layout_width="wrap_content"
193 android:layout_height="wrap_content"
194 android:layout_alignParentRight="true"
195 android:layout_marginRight="@dimen/margin_right"
196 android:orientation="horizontal" >
197
198 <RadioButton
199 android:id="@+id/trafficyesradio"
200 android:layout_width="wrap_content"
201 android:layout_height="wrap_content"
202 android:layout_marginRight="@dimen/margin_right"
203 android:checked="true"
204 android:text="@string/open"
205 android:textColor="@color/black" />
206
207 <RadioButton
208 android:id="@+id/trafficnoradio"
209 android:layout_width="wrap_content"
210 android:layout_height="wrap_content"
211 android:text="@string/close"
212 android:textColor="@color/black" />
213 </RadioGroup>
214 </RelativeLayout>
215
216 <RelativeLayout
217 android:layout_weight="1"
218 android:layout_width="match_parent"
219 android:layout_height="wrap_content"
220 android:gravity="center_vertical"
221 android:layout_marginBottom="@dimen/setting_margin_bottom"
222 android:layout_marginLeft="@dimen/navibar_margin_left"
223 android:layout_marginRight="@dimen/navibar_margin_right"
224 android:layout_marginTop="@dimen/setting_margin_top"
225 android:background="@drawable/whiteborder" >
226
227 <TextView
228 android:layout_width="wrap_content"
229 android:layout_height="wrap_content"
230 android:layout_marginLeft="@dimen/margin_left"
231 android:layout_marginTop="10dp"
232 android:text="@string/camerabroadcast"
233 android:textColor="@color/black"
234 android:textSize="@dimen/navisetting_subtext_size" />
235
236 <RadioGroup
237 android:id="@+id/camera_group"
238 android:layout_width="wrap_content"
239 android:layout_height="wrap_content"
240 android:layout_alignParentRight="true"
241 android:layout_marginRight="@dimen/margin_right"
242 android:orientation="horizontal" >
243
244 <RadioButton
245 android:id="@+id/camerayesradio"
246 android:layout_width="wrap_content"
247 android:layout_height="wrap_content"
248 android:layout_marginRight="@dimen/margin_right"
249 android:checked="true"
250 android:text="@string/open"
251 android:textColor="@color/black" />
252
253 <RadioButton
254 android:id="@+id/cameranoradio"
255 android:layout_width="wrap_content"
256 android:layout_height="wrap_content"
257 android:text="@string/close"
258 android:textColor="@color/black" />
259 </RadioGroup>
260 </RelativeLayout>
261
262 <RelativeLayout
263 android:layout_weight="1"
264 android:layout_width="match_parent"
265 android:layout_height="wrap_content"
266 android:gravity="center_vertical"
267 android:layout_marginBottom="@dimen/setting_margin_bottom"
268 android:layout_marginLeft="@dimen/navibar_margin_left"
269 android:layout_marginRight="@dimen/navibar_margin_right"
270 android:layout_marginTop="@dimen/setting_margin_top"
271 android:background="@drawable/whiteborder" >
272
273 <TextView
274 android:layout_width="wrap_content"
275 android:layout_height="wrap_content"
276 android:layout_marginLeft="@dimen/margin_left"
277 android:layout_marginTop="10dp"
278 android:text="@string/screenon"
279 android:textColor="@color/black"
280 android:textSize="@dimen/navisetting_subtext_size" />
281
282 <RadioGroup
283 android:id="@+id/screen_group"
284 android:layout_width="wrap_content"
285 android:layout_height="wrap_content"
286 android:layout_alignParentRight="true"
287 android:layout_marginRight="@dimen/margin_right"
288 android:orientation="horizontal" >
289
290 <RadioButton
291 android:id="@+id/screenonradio"
292 android:layout_width="wrap_content"
293 android:layout_height="wrap_content"
294 android:layout_marginRight="@dimen/margin_right"
295 android:checked="true"
296 android:text="@string/yes"
297 android:textColor="@color/black" />
298
299 <RadioButton
300 android:id="@+id/screenoffradio"
301 android:layout_width="wrap_content"
302 android:layout_height="wrap_content"
303 android:text="@string/no"
304 android:textColor="@color/black" />
305 </RadioGroup>
306 </RelativeLayout>
307
308 </LinearLayout>
View Code
实现代码:
1 package com.oysd.activity;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.view.KeyEvent;
7 import android.view.View;
8 import android.view.View.OnClickListener;
9 import android.widget.ImageView;
10 import android.widget.RadioGroup;
11 import android.widget.RadioGroup.OnCheckedChangeListener;
12
13 import com.amap.api.navi.AMapNaviViewOptions;
14 import com.oysd.Util.Utils;
15 import com.oysd.ouyangmap.R;
16 import com.oysd.ouyangmap.R.id;
17 import com.oysd.ouyangmap.R.layout;
18
19 /**
20 * 导航设置界面
21 *
22 */
23 public class NaviSettingActivity extends Activity implements OnClickListener,
24 OnCheckedChangeListener {
25 // ----------------View
26
27 private ImageView mBackView;//返回按钮
28 private RadioGroup mDayNightGroup;//黑夜模式白天模式
29 private RadioGroup mDeviationGroup;//偏航重算
30 private RadioGroup mJamGroup;//拥堵重算
31 private RadioGroup mTrafficGroup;//交通播报
32 private RadioGroup mCameraGroup;//摄像头播报
33 private RadioGroup mScreenGroup;//屏幕常亮
34
35 private boolean mDayNightFlag = Utils.DAY_MODE;
36 private boolean mDeviationFlag = Utils.YES_MODE;
37 private boolean mJamFlag = Utils.YES_MODE;
38 private boolean mTrafficFlag = Utils.OPEN_MODE;
39 private boolean mCameraFlag = Utils.OPEN_MODE;
40 private boolean mScreenFlag = Utils.YES_MODE;
41 private int mThemeStyle;
42
43 protected void onCreate(Bundle savedInstanceState) {
44 super.onCreate(savedInstanceState);
45 setContentView(R.layout.activity_navisetting);
46 Bundle bundle=getIntent().getExtras();
47 processBundle(bundle);
48 initView();
49 initListener();
50 }
51
52
53 /**
54 * 初始化控件
55 */
56 private void initView() {
57 mBackView = (ImageView) findViewById(R.id.setting_back_image);
58 mDayNightGroup = (RadioGroup) findViewById(R.id.day_night_group);
59 mDeviationGroup = (RadioGroup) findViewById(R.id.deviation_group);
60 mJamGroup = (RadioGroup) findViewById(R.id.jam_group);
61 mTrafficGroup = (RadioGroup) findViewById(R.id.traffic_group);
62 mCameraGroup = (RadioGroup) findViewById(R.id.camera_group);
63 mScreenGroup = (RadioGroup) findViewById(R.id.screen_group);
64
65 }
66
67 /**
68 * 初始化监听事件
69 */
70 private void initListener() {
71 mBackView.setOnClickListener(this);
72 mDayNightGroup.setOnCheckedChangeListener(this);
73 mDeviationGroup.setOnCheckedChangeListener(this);
74 mJamGroup.setOnCheckedChangeListener(this);
75 mTrafficGroup.setOnCheckedChangeListener(this);
76 mCameraGroup.setOnCheckedChangeListener(this);
77 mScreenGroup.setOnCheckedChangeListener(this);
78
79 }
80
81 /**
82 * 根据导航界面传过来的数据设置当前界面的显示状态
83 */
84 private void setViewContent() {
85 if (mDayNightGroup == null) {
86 return;
87 }
88 if (mDayNightFlag) {
89 mDayNightGroup.check(R.id.nightradio);
90 } else {
91 mDayNightGroup.check(R.id.dayratio);
92 }
93 if (mDeviationFlag) {
94 mDeviationGroup.check(R.id.deviationyesradio);
95 } else {
96 mDeviationGroup.check(R.id.deviationnoradio);
97 }
98
99 if (mJamFlag) {
100 mJamGroup.check(R.id.jam_yes_radio);
101 } else {
102 mJamGroup.check(R.id.jam_no_radio);
103 }
104
105 if (mTrafficFlag) {
106 mTrafficGroup.check(R.id.trafficyesradio);
107 } else {
108 mTrafficGroup.check(R.id.trafficnoradio);
109 }
110
111 if (mCameraFlag) {
112 mCameraGroup.check(R.id.camerayesradio);
113 } else {
114 mCameraGroup.check(R.id.cameranoradio);
115 }
116
117 if (mScreenFlag) {
118 mScreenGroup.check(R.id.screenonradio);
119 } else {
120 mScreenGroup.check(R.id.screenoffradio);
121 }
122 }
123
124 /**
125 * 处理具体的bundle
126 * @param bundle
127 */
128 private void processBundle(Bundle bundle) {
129 if (bundle != null) {
130 mThemeStyle = bundle.getInt(Utils.THEME,
131 AMapNaviViewOptions.DEFAULT_COLOR_TOPIC);
132 mDayNightFlag = bundle.getBoolean(Utils.DAY_NIGHT_MODE);
133 mDeviationFlag = bundle.getBoolean(Utils.DEVIATION);
134 mJamFlag = bundle.getBoolean(Utils.JAM);
135 mTrafficFlag = bundle.getBoolean(Utils.TRAFFIC);
136 mCameraFlag = bundle.getBoolean(Utils.CAMERA);
137 mScreenFlag = bundle.getBoolean(Utils.SCREEN);
138
139 }
140 }
141
142 /**
143 * 根据当前界面的设置设置,构建bundle
144 * @return
145 */
146 private Bundle getBundle() {
147 Bundle bundle = new Bundle();
148 bundle.putBoolean(Utils.DAY_NIGHT_MODE, mDayNightFlag);
149 bundle.putBoolean(Utils.DEVIATION, mDeviationFlag);
150 bundle.putBoolean(Utils.JAM, mJamFlag);
151 bundle.putBoolean(Utils.TRAFFIC, mTrafficFlag);
152 bundle.putBoolean(Utils.CAMERA, mCameraFlag);
153 bundle.putBoolean(Utils.SCREEN, mScreenFlag);
154 bundle.putInt(Utils.THEME, mThemeStyle);
155 return bundle;
156 }
157
158 // 事件处理方法
159 @Override
160 public void onClick(View v) {
161 switch (v.getId()) {
162 case R.id.setting_back_image:
163
164 Intent intent = new Intent(NaviSettingActivity.this,
165 NaviCustomActivity.class);
166 intent.putExtras(getBundle());
167 intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
168 startActivity(intent);
169 finish();
170 break;
171 }
172
173 }
174
175
176 /**
177 * 返回键监听
178 * */
179 @Override
180 public boolean onKeyDown(int keyCode, KeyEvent event) {
181
182 if (keyCode == KeyEvent.KEYCODE_BACK) {
183 Intent intent = new Intent(NaviSettingActivity.this,
184 NaviCustomActivity.class);
185 intent.putExtras(getBundle());
186 intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
187 startActivity(intent);
188 finish();
189
190 }
191 return super.onKeyDown(keyCode, event);
192 }
193
194 // ------------------------------生命周期重写方法---------------------------
195
196 @Override
197 public void onResume() {
198 // TODO Auto-generated method stub
199 super.onResume();
200 setViewContent();
201 }
202
203 @Override
204 public void onCheckedChanged(RadioGroup group, int checkedId) {
205 switch (checkedId) {
206 // 昼夜模式
207 case R.id.dayratio:
208 mDayNightFlag = Utils.DAY_MODE;
209 break;
210 case R.id.nightradio:
211 mDayNightFlag = Utils.NIGHT_MODE;
212 break;
213 // 偏航重算
214 case R.id.deviationyesradio:
215 mDeviationFlag = Utils.YES_MODE;
216 break;
217 case R.id.deviationnoradio:
218 mDeviationFlag = Utils.NO_MODE;
219 break;
220 // 拥堵重算
221 case R.id.jam_yes_radio:
222 mJamFlag = Utils.YES_MODE;
223 break;
224 case R.id.jam_no_radio:
225 mJamFlag = Utils.NO_MODE;
226 break;
227 // 交通播报
228 case R.id.trafficyesradio:
229 mTrafficFlag = Utils.OPEN_MODE;
230 break;
231 case R.id.trafficnoradio:
232 mTrafficFlag = Utils.CLOSE_MODE;
233 break;
234 // 摄像头播报
235 case R.id.camerayesradio:
236 mCameraFlag = Utils.OPEN_MODE;
237 break;
238 case R.id.cameranoradio:
239 mCameraFlag = Utils.CLOSE_MODE;
240 break;
241 // 屏幕常亮
242 case R.id.screenonradio:
243 mScreenFlag = Utils.YES_MODE;
244 break;
245 case R.id.screenoffradio:
246 mScreenFlag = Utils.NO_MODE;
247 break;
248 }
249
250 }
251 }
View Code
以上就差不多实现了导航的基本功能,后面开始分享地点搜索并且获得搜索点的经纬度
第八步:兴趣点搜索+地理编码+步行规划(只是使用简单的兴趣点搜索,勿喷,另外,我这里只是使用了步行导航,如果需要驾车导航以及公交导航,可以去调用相类似的方法)
首先需要在高德API官方网站下载搜索服务的jar包,至于在哪里,可以自己去查看,下载了这么多包了,应该知道怎么找了
添加包之后,就可以写代码了,以下是我的RoutePlanningActivity的布局文件:
1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent" >
5
6 <com.amap.api.maps.MapView
7 android:id="@+id/map"
8 android:layout_width="match_parent"
9 android:layout_height="match_parent" />
10
11 <LinearLayout
12 android:layout_width="match_parent"
13 android:layout_height="wrap_content"
14 android:orientation="vertical">
15
16 <LinearLayout
17 android:layout_width="fill_parent"
18 android:layout_height="match_parent"
19 android:orientation="horizontal">
20 <AutoCompleteTextView
21 android:id="@+id/startPlace"
22 android:layout_width="match_parent"
23 android:layout_height="wrap_content"
24 android:layout_weight="2"
25 android:hint="起点"
26 android:text="天虹商场" />
27 <Button
28 android:id="@+id/addStartPlace"
29 android:layout_width="match_parent"
30 android:layout_height="wrap_content"
31 android:layout_weight="4"
32 android:text="添加起点"/>
33 </LinearLayout>
34
35 <LinearLayout
36 android:layout_width="fill_parent"
37 android:layout_height="match_parent"
38 android:orientation="horizontal">
39 <AutoCompleteTextView
40 android:id="@+id/endPlace"
41 android:layout_width="match_parent"
42 android:layout_height="wrap_content"
43 android:layout_weight="2"
44 android:hint="终点"
45 android:text="上沙村" />
46 <Button
47 android:id="@+id/addEndPlace"
48 android:layout_width="match_parent"
49 android:layout_height="wrap_content"
50 android:layout_weight="4"
51 android:text="添加终点"/>
52 </LinearLayout>
53
54 <Button
55 android:id="@+id/searchBtn"
56 android:layout_width="match_parent"
57 android:layout_height="wrap_content"
58 android:text="搜索"/>
59
60 </LinearLayout>
61
62 </RelativeLayout>
View Code
然后就是RoutePlanningActivity的实现代码了:
1 package com.oysd.activity;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import com.amap.api.maps.AMap;
7 import com.amap.api.maps.MapView;
8 import com.amap.api.maps.overlay.WalkRouteOverlay;
9 import com.amap.api.services.core.AMapException;
10 import com.amap.api.services.core.LatLonPoint;
11 import com.amap.api.services.geocoder.GeocodeQuery;
12 import com.amap.api.services.geocoder.GeocodeResult;
13 import com.amap.api.services.geocoder.GeocodeSearch;
14 import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
15 import com.amap.api.services.geocoder.RegeocodeResult;
16 import com.amap.api.services.help.Inputtips;
17 import com.amap.api.services.help.Tip;
18 import com.amap.api.services.poisearch.PoiSearch;
19 import com.amap.api.services.route.BusRouteResult;
20 import com.amap.api.services.route.DriveRouteResult;
21 import com.amap.api.services.route.RouteSearch;
22 import com.amap.api.services.route.RouteSearch.OnRouteSearchListener;
23 import com.amap.api.services.route.WalkPath;
24 import com.amap.api.services.route.WalkRouteResult;
25 import com.oysd.ouyangmap.R;
26
27 import android.app.Activity;
28 import android.app.ProgressDialog;
29 import android.os.Bundle;
30 import android.text.Editable;
31 import android.text.TextWatcher;
32 import android.view.View;
33 import android.view.View.OnClickListener;
34 import android.widget.ArrayAdapter;
35 import android.widget.AutoCompleteTextView;
36 import android.widget.Button;
37 import android.widget.EditText;
38 import android.widget.Toast;
39
40 public class RoutePlanningActivity extends Activity implements
41 OnRouteSearchListener, OnGeocodeSearchListener, OnClickListener{
42
43 private MapView mMapView;
44 private AMap mAMap;
45
46 private AutoCompleteTextView startPlace;// 起点
47 private AutoCompleteTextView endPlace;// 终点
48 private Button searchBtn;// 搜索按钮
49
50 private Button addStartPlaceBtn;
51 private Button addEndPlaceBtn;
52 private boolean isStart;
53 private boolean isEnd;
54 private GeocodeSearch mGeocodeSearch;
55 // 搜索时进度条
56 private ProgressDialog progDialog = null;
57 private String strStart;
58 private String strEnd;
59 private LatLonPoint startPoint = null;
60 private LatLonPoint endPoint = null;
61 private PoiSearch.Query startSearchQuery;
62 private PoiSearch.Query endSearchQuery;
63
64 private int walkMode = RouteSearch.WalkDefault;// 步行默认模式
65 private RouteSearch routeSearch;
66
67 @Override
68 protected void onCreate(Bundle savedInstanceState) {
69 // TODO Auto-generated method stub
70 super.onCreate(savedInstanceState);
71 setContentView(R.layout.activity_routeplanning);
72 mMapView = (MapView) findViewById(R.id.map);
73 mMapView.onCreate(savedInstanceState);
74 initAMap();
75 findViewById();
76 }
77
78 /**
79 * 初始化AMap对象
80 */
81 private void initAMap() {
82 if (mAMap == null) {
83 mAMap = mMapView.getMap();
84 }
85 mGeocodeSearch = new GeocodeSearch(this);
86 mGeocodeSearch.setOnGeocodeSearchListener(this);
87
88 }
89
90 /**
91 * 获取控件
92 */
93 private void findViewById() {
94 startPlace = (AutoCompleteTextView) findViewById(R.id.startPlace);
95 endPlace = (AutoCompleteTextView) findViewById(R.id.endPlace);
96 searchBtn = (Button) findViewById(R.id.searchBtn);
97 addEndPlaceBtn = (Button) findViewById(R.id.addEndPlace);
98 addStartPlaceBtn = (Button) findViewById(R.id.addStartPlace);
99
100 setListener();
101 }
102
103 /**
104 * 设置监听
105 */
106 private void setListener() {
107
108 searchBtn.setOnClickListener(this);
109 addStartPlaceBtn.setOnClickListener(this);
110 addEndPlaceBtn.setOnClickListener(this);
111 startPlace.addTextChangedListener(this.startPlaceWatch);
112 endPlace.addTextChangedListener(this.endPlaceWatch);
113 }
114
115 TextWatcher startPlaceWatch = new TextWatcher() {
116
117 @Override
118 public void onTextChanged(CharSequence s, int start, int before, int count) {
119 // TODO Auto-generated method stub
120 String newText = s.toString().trim();
121 Inputtips inputTips = new Inputtips(RoutePlanningActivity.this,
122 new Inputtips.InputtipsListener() {
123 @Override
124 public void onGetInputtips(List<Tip> tipList, int rCode) {
125 if (rCode == 0) {// 正确返回
126 List<String> listString = new ArrayList<String>();
127 for (int i = 0; i < tipList.size(); i++) {
128 listString.add(tipList.get(i).getName());
129 }
130 ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(RoutePlanningActivity.this,android.R.layout.simple_list_item_1, listString);
131 startPlace.setAdapter(aAdapter);
132 aAdapter.notifyDataSetChanged();
133 }
134 }
135 });
136 try {
137 // 第一个参数表示提示关键字,第二个参数默认代表全国,也可以为城市区号
138 inputTips.requestInputtips(newText,null);
139 } catch (AMapException e) {
140 e.printStackTrace();
141 }
142 }
143
144 @Override
145 public void beforeTextChanged(CharSequence s, int start, int count,
146 int after) {
147 // TODO Auto-generated method stub
148
149 }
150
151 @Override
152 public void afterTextChanged(Editable s) {
153 // TODO Auto-generated method stub
154
155 }
156 };
157
158 TextWatcher endPlaceWatch = new TextWatcher() {
159
160 @Override
161 public void onTextChanged(CharSequence s, int start, int before, int count) {
162 // TODO Auto-generated method stub
163 String newText = s.toString().trim();
164 Inputtips inputTips = new Inputtips(RoutePlanningActivity.this,
165 new Inputtips.InputtipsListener() {
166 @Override
167 public void onGetInputtips(List<Tip> tipList, int rCode) {
168 if (rCode == 0) {// 正确返回
169 List<String> listString = new ArrayList<String>();
170 for (int i = 0; i < tipList.size(); i++) {
171 listString.add(tipList.get(i).getName());
172 }
173 ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(RoutePlanningActivity.this,android.R.layout.simple_list_item_2, listString);
174 startPlace.setAdapter(aAdapter);
175 aAdapter.notifyDataSetChanged();
176 }
177 }
178 });
179 try {
180 // 第一个参数表示提示关键字,第二个参数默认代表全国,也可以为城市区号
181 inputTips.requestInputtips(newText,null);
182 } catch (AMapException e) {
183 e.printStackTrace();
184 }
185 }
186
187 @Override
188 public void beforeTextChanged(CharSequence s, int start, int count,
189 int after) {
190 // TODO Auto-generated method stub
191
192 }
193
194 @Override
195 public void afterTextChanged(Editable s) {
196 // TODO Auto-generated method stub
197
198 }
199 };
200
201 /**
202 * 显示进度框
203 */
204 private void showProgressDialog() {
205 if (progDialog == null)
206 progDialog = new ProgressDialog(this);
207 progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
208 progDialog.setIndeterminate(false);
209 progDialog.setCancelable(false);
210 progDialog.setMessage("正在规划:\n");
211 progDialog.show();
212 }
213
214 /**
215 * 隐藏进度框
216 */
217 private void dissmissProgressDialog() {
218 if (progDialog != null) {
219 progDialog.dismiss();
220 }
221 }
222
223 @Override
224 public void onBusRouteSearched(BusRouteResult arg0, int arg1) {
225 // TODO Auto-generated method stub
226
227 }
228
229 @Override
230 public void onDriveRouteSearched(DriveRouteResult arg0, int arg1) {
231 // TODO Auto-generated method stub
232
233 }
234
235 @Override
236 public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int rCode) {
237 // TODO Auto-generated method stub
238 dissmissProgressDialog();
239 if (rCode == 0) {
240 if (walkRouteResult != null && walkRouteResult.getPaths() != null
241 && walkRouteResult.getPaths().size() > 0) {
242 WalkPath walkPath = walkRouteResult.getPaths().get(0);
243
244 mAMap.clear();// 清理之前的图标
245 WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(this,
246 mAMap, walkPath, walkRouteResult.getStartPos(),
247 walkRouteResult.getTargetPos());
248 walkRouteOverlay.removeFromMap();
249 walkRouteOverlay.addToMap();
250 walkRouteOverlay.zoomToSpan();
251 } else {
252 dissmissProgressDialog();// 隐藏对话框
253 Toast.makeText(RoutePlanningActivity.this, "没有搜索到结果",
254 Toast.LENGTH_SHORT).show();
255 }
256 } else {
257 dissmissProgressDialog();// 隐藏对话框
258 Toast.makeText(RoutePlanningActivity.this, "网络错误",
259 Toast.LENGTH_SHORT).show();
260 }
261 }
262
263 @Override
264 public void onGeocodeSearched(GeocodeResult geocodeResult, int rCode) {
265 // TODO Auto-generated method stub
266 if (rCode == 0) {
267 if (geocodeResult != null
268 && geocodeResult.getGeocodeAddressList() != null
269 && geocodeResult.getGeocodeAddressList().size() > 0) {
270 LatLonPoint latLonPoint = geocodeResult.getGeocodeAddressList()
271 .get(0).getLatLonPoint();
272 if (isStart) {
273 startPoint = new LatLonPoint(latLonPoint.getLatitude(),
274 latLonPoint.getLongitude());
275 Toast.makeText(RoutePlanningActivity.this, "加入起点成功",
276 Toast.LENGTH_SHORT).show();
277 } else if (isEnd) {
278 endPoint = new LatLonPoint(latLonPoint.getLatitude(),
279 latLonPoint.getLongitude());
280 Toast.makeText(RoutePlanningActivity.this, "加入终点成功",
281 Toast.LENGTH_SHORT).show();
282 }
283 } else {
284 Toast.makeText(RoutePlanningActivity.this, "没有结果",
285 Toast.LENGTH_SHORT).show();
286 }
287
288 } else {
289 Toast.makeText(RoutePlanningActivity.this, "网络连接失败",
290 Toast.LENGTH_SHORT).show();
291 }
292 }
293
294 @Override
295 public void onRegeocodeSearched(RegeocodeResult arg0, int arg1) {
296 // TODO Auto-generated method stub
297
298 }
299
300 @Override
301 public void onClick(View v) {
302 // TODO Auto-generated method stub
303 switch(v.getId()){
304 case R.id.searchBtn:
305 doSearch();
306 break;
307 case R.id.addStartPlace:
308 addStartPlace();
309 break;
310 case R.id.addEndPlace:
311 addEndPlace();
312 break;
313 }
314 }
315
316 /**
317 * 执行搜索功能
318 */
319 private void doSearch(){
320 showProgressDialog();// 显示进度框
321 RouteSearch.FromAndTo fromAndto = new RouteSearch.FromAndTo(
322 startPoint, endPoint);
323 routeSearch = new RouteSearch(RoutePlanningActivity.this);
324 routeSearch.setRouteSearchListener(RoutePlanningActivity.this);
325 RouteSearch.WalkRouteQuery query = new RouteSearch.WalkRouteQuery(
326 fromAndto, walkMode);
327 routeSearch.calculateWalkRouteAsyn(query);
328 }
329
330 /**
331 * 执行添加起点操作功能
332 */
333 private void addStartPlace(){
334
335 isStart = true;
336 isEnd = false;
337 addPoint(startPlace.getText().toString().trim());
338 }
339
340
341 /**
342 * 执行添加终点操作功能
343 */
344 private void addEndPlace(){
345 isStart = false;
346 isEnd = true;
347 addPoint(endPlace.getText().toString().trim());
348 }
349
350 private void addPoint(String address){
351
352 // 第一个参数表示地址,第二个参数表示查询城市,中文或者中文全拼,citycode、adcode
353 GeocodeQuery query = new GeocodeQuery(address, "深圳");
354 mGeocodeSearch.getFromLocationNameAsyn(query);
355 }
356
357 }
View Code
不过这里面使用了AutoCompleteTextView 来实现输入提示,里面有一点点的问题,我这里有输入起点与终点,
输入起点的时候,会有输入提示,但是输入终点就没有任何提示,自己也是不知道问题出在哪里,若有大神飘过,还请帮我解惑
到这里 这个高德地图的简单功能开发算是分享得差不多了。
其实,高德地图、百度地图、谷歌地图这些的开发都大同小异,还是需要认真查看API文档
如果可能的话,完全可以在自己的手机上安装一个属于自己的私人地图,想要什么功能,自己往里面添加,自己怎么用的方便怎么用。。。。