演示Android百度地图操作功能
在本文中将演示百度地图的操作功能,包括缩放,旋转,视角切换,点击,双击,长按事件触发的操作以及截图等。百度地图本来就内置有缩放,旋转功能,那么在这里,截图(其实很多手机也自带截图功能)以及点击事件的监听算是比较有实际意义的功能。代码原型来源百度demo:
Activity:
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
package com.home; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.map.MKMapTouchListener; import com.baidu.mapapi.map.MKMapViewListener; import com.baidu.mapapi.map.MapController; import com.baidu.mapapi.map.MapPoi; import com.baidu.mapapi.map.MapView; import com.baidu.platform.comapi.basestruct.GeoPoint; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class ControlBMapActivity extends Activity { /** * MapView 是地图主控件 */ private MapView mMapView = null ; /** * 用MapController完成地图控制 */ private MapController mMapController = null ; /** * MKMapViewListener 用于处理地图事件回调 */ MKMapViewListener mMapListener = null ; /** * 用于截获屏坐标 */ MKMapTouchListener mapTouchListener = null ; /** * 当前地点击点 */ private GeoPoint currentPt = null ; /** * 控制按钮 */ private Button zoomButton = null ; private Button rotateButton = null ; private Button overlookButton = null ; private Button saveScreenButton = null ; private String touchType = null ; /** * 用于显示地图状态的面板 */ private TextView mStateTextView = null ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); DemoApplication app = (DemoApplication) this .getApplication(); if (app.mBMapManager == null ) { app.mBMapManager = new BMapManager( this ); /** * 如果BMapManager没有初始化则初始化BMapManager */ app.mBMapManager.init(DemoApplication.strKey, new DemoApplication.MyGeneralListener()); } setContentView(R.layout.control_main); mMapView = (MapView) findViewById(R.id.bmapView); /** * 获取地图控制器 */ mMapController = mMapView.getController(); /** * 设置地图是否响应点击事件 . */ mMapController.enableClick( true ); /** * 设置地图缩放级别 */ mMapController.setZoom( 12 ); mStateTextView = (TextView) findViewById(R.id.state); /** * 初始化地图事件监听 */ initListener(); /** * 将地图默认移动至天安门 */ double cLat = 39.945 ; double cLon = 116.404 ; GeoPoint p = new GeoPoint(( int ) (cLat * 1E6), ( int ) (cLon * 1E6)); mMapController.setCenter(p); } private void initListener() { /** * 设置地图点击事件监听 */ mapTouchListener = new MKMapTouchListener() { @Override public void onMapClick(GeoPoint point) { touchType = "单击" ; currentPt = point; updateMapState(); } @Override public void onMapDoubleClick(GeoPoint point) { touchType = "双击" ; currentPt = point; updateMapState(); } @Override public void onMapLongClick(GeoPoint point) { touchType = "长按" ; currentPt = point; updateMapState(); } }; mMapView.regMapTouchListner(mapTouchListener); /** * 设置地图事件监听 */ mMapListener = new MKMapViewListener() { @Override public void onMapMoveFinish() { /** * 在此处理地图移动完成回调 缩放,平移等操作完成后,此回调被触发 */ updateMapState(); } @Override public void onClickMapPoi(MapPoi mapPoiInfo) { /** * 在此处理底图poi点击事件 显示底图poi名称并移动至该点 设置过: * mMapController.enableClick(true); 时,此回调才能被触发 * */ } @Override public void onGetCurrentMap(Bitmap b) { /** * 当调用过 mMapView.getCurrentMap()后,此回调会被触发 可在此保存截图至存储设备 * */ if (!Environment.MEDIA_MOUNTED.equals(Environment .getExternalStorageState())) { Toast.makeText(ControlBMapActivity. this , "请插入SD卡" , Toast.LENGTH_SHORT).show(); return ; } File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".png" ); FileOutputStream out; try { out = new FileOutputStream(file); if (b.compress(Bitmap.CompressFormat.PNG, 70 , out)) { out.flush(); out.close(); } Toast.makeText(ControlBMapActivity. this , "屏幕截图成功,图片存在: " + file.toString(), Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onMapAnimationFinish() { /** * 地图完成带动画的操作(如: animationTo())后,此回调被触发 */ updateMapState(); } @Override public void onMapLoadFinish() { } }; mMapView.regMapViewListener(DemoApplication.getInstance().mBMapManager, mMapListener); /** * 设置按键监听 */ zoomButton = (Button) findViewById(R.id.zoombutton); rotateButton = (Button) findViewById(R.id.rotatebutton); overlookButton = (Button) findViewById(R.id.overlookbutton); saveScreenButton = (Button) findViewById(R.id.savescreen); OnClickListener onClickListener = new OnClickListener() { @Override public void onClick(View view) { if (view.equals(zoomButton)) { perfomZoom(); } else if (view.equals(rotateButton)) { perfomRotate(); } else if (view.equals(overlookButton)) { perfomOverlook(); } else if (view.equals(saveScreenButton)) { // 截图,在MKMapViewListener中保存图片 mMapView.getCurrentMap(); Toast.makeText(ControlBMapActivity. this , "正在截取屏幕图片..." , Toast.LENGTH_SHORT).show(); } updateMapState(); } }; zoomButton.setOnClickListener(onClickListener); rotateButton.setOnClickListener(onClickListener); overlookButton.setOnClickListener(onClickListener); saveScreenButton.setOnClickListener(onClickListener); } /** * 处理缩放,sdk缩放级别范围:[3.0,19.0],小于3.0将按3.0处理;大于19.0将按19.0处理 */ private void perfomZoom() { EditText t = (EditText) findViewById(R.id.zoomlevel); try { float zoomLevel = Float.parseFloat(t.getText().toString()); mMapController.setZoom(zoomLevel); } catch (NumberFormatException e) { Toast.makeText( this , "请输入正确的缩放级别,范围: [3.0,19.0],只能输入浮点型或整型" , Toast.LENGTH_SHORT).show(); } } /** * 处理旋转 ,旋转角单位:度 ,逆时针旋转 */ private void perfomRotate() { EditText t = (EditText) findViewById(R.id.rotateangle); try { int rotateAngle = Integer.parseInt(t.getText().toString()); mMapController.setRotation(rotateAngle); } catch (NumberFormatException e) { Toast.makeText( this , "请输入正确的旋转角度,只能为整型" , Toast.LENGTH_SHORT).show(); } } /** * 处理俯视 俯角范围: -45 ~ 0 , 单位: 度 */ private void perfomOverlook() { EditText t = (EditText) findViewById(R.id.overlookangle); try { int overlookAngle = Integer.parseInt(t.getText().toString()); mMapController.setOverlooking(overlookAngle); } catch (NumberFormatException e) { Toast.makeText( this , "请输入正确的俯角,范围: -45 ~ 0" , Toast.LENGTH_SHORT) .show(); } } /** * 更新地图状态显示面板 */ private void updateMapState() { if (mStateTextView == null ) { return ; } String state = "" ; if (currentPt == null ) { state = "点击、长按、双击地图以获取经纬度和地图状态" ; } else { state = String.format(touchType + ",当前经度 : %f 当前纬度:%f" , currentPt.getLongitudeE6() * 1E- 6 , currentPt.getLatitudeE6() * 1E- 6 ); } state += "\n" ; state += String .format( "zoom level= %.1f rotate angle= %d overlaylook angle= %d" , mMapView.getZoomLevel(), mMapView.getMapRotation(), mMapView.getMapOverlooking()); mStateTextView.setText(state); } @Override protected void onPause() { /** * MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause() */ mMapView.onPause(); super .onPause(); } @Override protected void onResume() { /** * MapView的生命周期与Activity同步,当activity恢复时需调用MapView.onResume() */ mMapView.onResume(); super .onResume(); } @Override protected void onDestroy() { /** * MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy() */ mMapView.destroy(); super .onDestroy(); } @Override protected void onSaveInstanceState(Bundle outState) { super .onSaveInstanceState(outState); mMapView.onSaveInstanceState(outState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super .onRestoreInstanceState(savedInstanceState); mMapView.onRestoreInstanceState(savedInstanceState); } } |
布局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
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
|
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < LinearLayout android:id = "@+id/layout_bottom" android:layout_width = "match_parent" android:layout_height = "50dip" android:layout_alignParentBottom = "true" android:orientation = "horizontal" > < Button android:id = "@+id/zoombutton" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_weight = "1" android:text = "缩放" /> < EditText android:id = "@+id/zoomlevel" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_weight = "1" android:text = "10" /> < Button android:id = "@+id/rotatebutton" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_weight = "1" android:text = "旋转" /> < EditText android:id = "@+id/rotateangle" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_weight = "1" android:numeric = "integer" android:text = "90" /> < Button android:id = "@+id/overlookbutton" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_weight = "1" android:text = "俯视" /> < EditText android:id = "@+id/overlookangle" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_weight = "1" android:text = "-30" /> </ LinearLayout > < TextView android:id = "@+id/state" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_above = "@id/layout_bottom" android:text = "点击、长按、双击地图以获取经纬度和地图状态" /> < RelativeLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_above = "@id/state" android:orientation = "vertical" > < com.baidu.mapapi.map.MapView android:id = "@+id/bmapView" android:layout_width = "match_parent" android:layout_height = "match_parent" android:clickable = "true" /> < Button android:id = "@+id/savescreen" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignParentRight = "true" android:layout_alignParentTop = "true" android:layout_marginTop = "10dip" android:text = "截图" /> </ RelativeLayout > </ RelativeLayout > |
Application类及Manifest同上文。
附上图片效果:
演示Android百度地图操作功能的更多相关文章
- Android 百度鹰眼轨迹SDK(v2.1.6)
闲聊 看过<鹰眼追击>这部电影的读者一定对"鹰眼"这台巨无霸计算机印象深刻,如今我们能够实现自己的鹰眼. 效果图 本篇为百度地图SDK第三篇博文 第一篇实现:Andro ...
- Android 百度地图开发(一)--- 申请API Key和在项目中显示百度地图
标签: Android百度地图API Key 分类: Android 百度地图开发(2) 最近自己想研究下地图,本来想研究google Map,但是申请API key比较坑爹,于是从百度地 ...
- Android百度地图
帖子 热搜: 二维码 聊天 二维码扫描 传感器 游戏 定位 手势绘图 小项目 相框 绘图 涂鸦 拨打电话 记事本 定时器 通话记录 短信群发 listview 音乐播放器 项目例子 百度地 ...
- Android 百度地图API(01)_开发环境 HelloBaiduMap
转载于:http://blog.csdn.net/lmj623565791/article/details/37729091 转载于:http://blog.csdn.net/crazy1235/ar ...
- 集成Android免费语音合成功能(在线、离线、离在线融合)
集成Android免费语音合成功能(在线.离线.离在线融合),有这一篇文章就够了(离线)集成Android免费语音合成功能(在线.离线.离在线融合),有这一篇文章就够了(离在线融合) 转眼间,大半年没 ...
- Android 演示 Android ListView 和 github XListView(3-3)
本文内容 环境 项目结构 演示 1:简单 XListView 演示 2:XListView + Fragment 演示 3:XListView + ViewPager + Fragment 本文三个演 ...
- Android 演示 Android ListView 和 github XListView(1-3)
本文内容 环境 项目结构 演示 1:ListView 演示 2:简单 XListView 演示 3:音乐列表 XListView 演示 4:另一个音乐列表 XListView 本文四个演示,循序渐进. ...
- Android百度地图(二)结合方向传感器我们自己定位哪里走
Android百度地图(二)结合方向传感器我们自己定位哪里走 本文代码在http://blog.csdn.net/xyzz609/article/details/51943556的基础上进一步改动.有 ...
- (转载) Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框
Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框 标签: Android清除功能EditText仿IOS的输入框 2013-09-04 17:33 70865人阅读 ...
随机推荐
- Objective-C之NSArray(数组)默认排序与自定义排序
在讲OC中数组的排序之前我先上一段代码,它是简单数组排序的一种方法(也就是元素是字符串或者数据的数组,因为后面要讲元素为类的数组排序) 代码1: NSArray *sortArr4 = [sortAr ...
- matlab连接sql数据库
最近项目还涉及到matlab连接数据库,下面我就记录如何进行配置使得matlab能够连接sql数据库.由于最近工程做的多一些,所以分享的都在工程配置上,当初为了这些配置可是反复卸载与重装,算法其实也有 ...
- C# Get/Post 模拟提交
public static string GetPage(string url, string encoding) { PublicVariables.NetworkConnection = fals ...
- CentOS7安装hive-2.1.0
环境: CentOS7 Hadoop-2.6.4,配置两个节点:master.slave1 mysql-server 过程: 下载.解压hive-2.1.0到/usr/hadoop-2.6.4/thi ...
- socket传数据并记录到文件中
最近在新项目中要通过socket传一些数据,下面是程序: 功能: 将客户端发送的json数据写入到日志文件中,如果数据不是json的,丢弃. 程序如下: #!/usr/bin/env python # ...
- GUI1_综合介绍
最终比较,选择pyqt用于GUI开发 https://pythonspot.com/en/gui/ 图形化界面可以使用PyQt5, PyQt4, wxPython or Tk.模板 Graphical ...
- OC面向对象特性: 继承
基础知识 1.标识符是有字母,数字,下划线组成的. 2.首字母只能是字母,下划线,不能为数字. 3.标识符要做到见名之意. 4.标识符不能使用已定义的关键字和预定义标识符. 继承 继承:子类可以直接访 ...
- Java Web的两种开发模式
参考文献:http://www.cnblogs.com/xdp-gacl/p/3908610.html 一.Jsp+JavaBean 此模式如下图所示:
- Cloudservice程序设置Idle timeout
部署的云服务程序,默认的idle timeout是4分钟,意味着如果你通过一个workerrole发布了wcf服务,客户端第一次调用服务方法后,再过4分钟尝试去重新调用服务,会报错,具体测试如下: 1 ...
- cmake 编译 c++ dll 的一个例子(更新1)
CMakeLists.txt project(xxx) add_library(xxx SHARED xxx.cpp) add_executable(yyy yyy.cpp) target_link_ ...