Android 百度地图API 定位 导航
看看这个利用百度地图定位并实现目的地导航的Demo。
首先看实现效果:

进 入后首先会得到当前位置,在地图上显示出来。在输入框中输入目的地后,就会在地图上出现最佳线路,我这里设置的是距离最小的驾车线路,另外还有公交线路、 步行线路,在代码中都有具体凝视。另外,在控制台还输出了线路上每个节点的信息以及起始位置和目的地的距离,信息显示的是在当前节点的导航信息。例如以下 图:

接下来就看怎样实现了,首先。注冊百度开发人员账号,并进入百度地图API查看相关资料百度地图API,然后就是为须要增加地图的应用注冊APP
KEY,注冊完后,下载百度地图jar文件。新建project。并导入就可以。以下看实现具体代码,在代码中有具体凝视:
|
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 extends private"注冊自己的key"; privatenull; privatenull; privatenull; privatenull; privatenull; //onResume时注冊此listener,onPause时须要Remove,注意此listener不是Android自带的,是百度API中的 private private GeoPoint @Override public super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); destinationEditTextthis.findViewById(R.id.et_destination); startNaviButtonthis.findViewById(R.id.btn_navi); mMapManagernew mMapManager.init(mMapKey, new super.initMapActivity(mMapManager); mapViewthis.findViewById(R.id.bmapsView); //设置启用内置的缩放控件 mapView.setBuiltInZoomControls(true); //设置在缩放动画过程中也显示overlay,默觉得不绘制 // //获取当前位置层 myLocationOverlaynewthis, //将当前位置的层加入到地图底层中 mapView.getOverlays().add(myLocationOverlay); // locationListenernew @Override public ifnull){ //生成GEO类型坐标并在地图上定位到该坐标标示的地点 ptnewint)(location.getLatitude()*1e6), (int)(location.getLongitude()*1e6)); // mapView.getController().animateTo(pt); } } }; //初始化搜索模块 searchModelnew //设置路线策略为最短距离 searchModel.setDrivingPolicy(MKSearch.ECAR_DIS_FIRST); searchModel.init(mMapManager, new //获取驾车路线回调方法 @Override public int // if0null) Toast.makeText(NavigationDemoActivity.this, "抱歉,未找到结果", return; } RouteOverlaynewthis, // MKRoute0).getRoute(0); int String1000)"."+String.valueOf(distanceM1000); System.out.println("距离:"+distanceKm+"公里---节点数量:"+route.getNumSteps()); forint0; MKStep 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 int //获取步行路线 } @Override public int //获取公交线路 } @Override public int } @Override public int } @Override public int } @Override public intint } }); startNaviButton.setOnClickListener(new @Override public String //设置起始地(当前位置) MKPlanNodenew startNode.pt //设置目的地 MKPlanNodenew endNode.name //展开搜索的城市 String// searchModel.drivingSearch(city, //步行路线 // //公交路线 // } }); } @Override protected mMapManager.getLocationManager().requestLocationUpdates(locationListener); myLocationOverlay.enableMyLocation(); myLocationOverlay.enableCompass(); // mMapManager.start(); super.onResume(); } @Override protected mMapManager.getLocationManager().removeUpdates(locationListener); myLocationOverlay.disableMyLocation();//显示当前位置 myLocationOverlay.disableCompass(); // mMapManager.stop(); super.onPause(); } @Override protected // return; } // classimplements @Override public int Log.d("MyGeneralListener", "onGetNetworkState+ Toast.makeText(NavigationDemoActivity.this, "您的网络出错啦!
, Toast.LENGTH_LONG).show(); } @Override public int Log.d("MyGeneralListener", "onGetPermissionState+ if // Toast.makeText(NavigationDemoActivity.this, "请在BMapApiDemoApp.java文件输入正确的授权Key!
, 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="1.0"="utf-8"?> <LinearLayout="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/> <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="1.0"="utf-8"?> <manifest="http://schemas.android.com/apk/res/android" package="com.ericssonlabs" android:versionCode="1" android:versionName="1.0" <uses-sdk="8" <uses-permission="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission="android.permission.INTERNET"></uses-permission> <uses-permission="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission="android.permission.ACCESS_WIFI_STATE"></uses-permission> <uses-permission="android.permission.CHANGE_WIFI_STATE"></uses-permission> <uses-permission="android.permission.READ_PHONE_STATE"></uses-permission> <supports-screens="true" android:normalScreens="true"="true" android:resizeable="true"="true"/> <uses-sdk="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.intent.action.MAIN" <category="android.intent.category.LAUNCHER" </intent-filter> </activity> </application> </manifest> |
Android 百度地图API 定位 导航的更多相关文章
- 百度地图api定位和导航简写
function locate() { // 百度地图API功能 var map = new BMap.Map("allmap"); // 创建Map实例 var point = ...
- Android 百度地图API(01)_开发环境 HelloBaiduMap
转载于:http://blog.csdn.net/lmj623565791/article/details/37729091 转载于:http://blog.csdn.net/crazy1235/ar ...
- H5微信通过百度地图API实现导航方式一
根据业务需求修改百度API,实现微信中的导航功能.因为源码中SearchInfoWindow_min.js有点小问题(部分小城市公交线路少,查不到路线时没有提示),所以这里在源码的基础上改了一点点.可 ...
- 百度地图API定位+显示位置
1. 先在需要嵌入地图的页面引入map.js <script src="http://api.map.baidu.com/api?v=2.0&ak=你的秘钥"> ...
- Android应用中使用百度地图API定位自己的位置(二)
官方文档:http://developer.baidu.com/map/sdkandev-6.htm#.E7.AE.80.E4.BB.8B3 百度地图SDK为开发人员们提供了例如以下类型的地图覆盖物: ...
- 通过百度地图API定位--第三方开源--百度地图(一)
1.把百度地图定位API(下载地址:http://lbsyun.baidu.com/sdk/download?selected=location)里面的libs复制到自己的项目libs里面 2.进行相 ...
- 团队项目:安卓端用百度地图api定位显示跑道
因为安卓调用api对我来说是一个完全陌生的领域,我在经过很长时间终于弄出来了,这段时间还是很有成效的,我得到了历练. 第一步:注册成为百度开发者 在百度地图开放平台创建应用.地址http://lbsy ...
- 百度地图API 定位一直4.9E-324
使用百度地图Android SDK 7.0定位坐标一直为4.9E-324,网上搜索了很多,但是均未解决我的问题,在此坐下解决记录,在设置权限中将应用添加信任即可!android 6.0 其他版本未测! ...
- Android 百度地图SDK 定位
引用locSDK_6.1.3.jar,切记添加相应的so文件. 1.定位初始化,需要使用getApplicationContext() mLocClient = new LocationClient( ...
随机推荐
- 【POJ】2104 K-th Number
区间第K大数.主席树可解. /* 2104 */ #include <iostream> #include <sstream> #include <string> ...
- [POJ 2429] GCD & LCM Inverse
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10621 Accepted: ...
- apache开源项目--lume
lume 是一个分布式.可靠和高可用的服务,用于收集.聚合以及移动大量日志数据,使用一个简单灵活的架构,就流数据模型.这是一个可靠.容错的服务.
- 【原创】Linux下使用SecureCRT的方法:破解&编码
1.下载SecureCRT软件 ubuntu64位:http://download.csdn.net/detail/cobps/7941145 ubuntu32位:http://download.cs ...
- Parallel for loops in .NET C# z
The start index: this is inclusive, i.e. this will be the first index value in the loop The end inde ...
- HDU5692 Snacks DFS+线段树
分析:一棵以1为根的有根树,然后每个点维护从根到当前节点的路径和,当修改一个点时 只会影响的子树的和,最优值也是子树最大的值 #include <cstdio> #include < ...
- oracle ref游标
Oracle 系列:REF Cursor 在上文 Oracle 系列:Cursor (参见:http://blog.csdn.net/qfs_v/archive/2008/05/06/240479 ...
- uva 12296 Pieces and Discs
题意: 有个矩形,左下角(0,0),左上角(L,W). 思路: 除了圆盘之外,本题的输入也是个PSLG,因此可以按照前面叙述的算法求出各个区域:只需把线段视为直线,用切割凸多边形的方法 :每次读入线段 ...
- Bzoj 4556: [Tjoi2016&Heoi2016]字符串
4556: [Tjoi2016&Heoi2016]字符串 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 177 Solved: 92[Sub ...
- leetcode—Same Tree
1.题目描述 Given two binary trees, write a function to check if they are equal or not. Two binary tree ...