一:百度地图开发必须要到百度开发平台android开发api下载相应的库,已经申请百度地图开发key.

二:新建项目baidumaplocation.设计main.xml文件这里注意的是MapView控件必须使用来自百度库封装好的com.baidu.mapapi.MapView 。设计代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <FrameLayout
  7. android:id="@+id/map_layout"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:orientation="vertical" >
  11. <!-- 百度MapView控件 -->
  12. <com.baidu.mapapi.MapView
  13. android:id="@+id/map_view"
  14. android:layout_width="fill_parent"
  15. android:layout_height="fill_parent"
  16. android:apiKey="0Mg_koWoyZUiYLfZxmPfp4LKInB5LqTnagYueaw"
  17. android:clickable="true"
  18. android:enabled="true" />
  19. <LinearLayout
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_gravity="center"
  23. android:orientation="vertical"
  24. android:paddingBottom="105dip" >
  25. <!-- 地址信息显示TextView -->
  26. <TextView
  27. android:id="@+id/map_bubbleText"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:background="@drawable/location_tips"
  31. android:gravity="left|center"
  32. android:maxEms="12"
  33. android:paddingLeft="12dip"
  34. android:paddingRight="10dip"
  35. android:text="@string/load_tips"
  36. android:textColor="#cfcfcf"
  37. android:textSize="14sp" />
  38. </LinearLayout>
  39. <LinearLayout
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_gravity="center"
  43. android:orientation="vertical" >
  44. <!-- 位置指标显示ImageView -->
  45. <ImageView
  46. android:id="@+id/point_image"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:layout_gravity="center"
  50. android:layout_marginBottom="30dip"
  51. android:src="@drawable/point_start" />
  52. </LinearLayout>
  53. </FrameLayout>
  54. </LinearLayout>

三:创建覆盖整个地图捕捉触控事件的MyMapOverlay继承Overlay

  1. import android.view.MotionEvent;
  2. import com.baidu.mapapi.GeoPoint;
  3. import com.baidu.mapapi.MapView;
  4. import com.baidu.mapapi.Overlay;
  5. //覆盖整个地图捕捉触控事件的OverLay
  6. public abstract class MyMapOverlay extends Overlay{
  7. private int point_X;
  8. private int point_Y;
  9. private GeoPoint newPoint;
  10. public MyMapOverlay(int x,int y){
  11. point_X = x;
  12. point_Y = y;
  13. }
  14. boolean flagMove=false;
  15. //这里实现根据地图移动时重新获取屏幕中心点的经纬度坐标
  16. @Override
  17. public boolean onTouchEvent(MotionEvent event, MapView mapView) {
  18. System.out.println("X->"+event.getX()+":"+point_X);
  19. System.out.println("Y->"+event.getY()+":"+point_Y);
  20. if(event.getAction() == MotionEvent.ACTION_DOWN){
  21. changePoint(newPoint,1);
  22. }else if(event.getAction() == MotionEvent.ACTION_UP){
  23. newPoint = mapView.getProjection().fromPixels(point_X,point_Y);
  24. changePoint(newPoint,2);
  25. }
  26. return false;
  27. }
  28. public abstract void changePoint(GeoPoint newPoint,int type);
  29. }

四:LocationActivity类继承百度库的MapActivity以及实现LocationListener接口,代码如下:

package com.location.activity;

  1. import java.io.IOException;
  2. import java.util.List;
  3. import java.util.Locale;
  4. import android.content.Intent;
  5. import android.location.Address;
  6. import android.location.Geocoder;
  7. import android.location.Location;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.os.Message;
  11. import android.view.View;
  12. import android.view.Window;
  13. import android.widget.TextView;
  14. import com.android.map.MyMapOverlay;
  15. import com.baidu.mapapi.BMapManager;
  16. import com.baidu.mapapi.GeoPoint;
  17. import com.baidu.mapapi.LocationListener;
  18. import com.baidu.mapapi.MKAddrInfo;
  19. import com.baidu.mapapi.MKBusLineResult;
  20. import com.baidu.mapapi.MKDrivingRouteResult;
  21. import com.baidu.mapapi.MKLocationManager;
  22. import com.baidu.mapapi.MKPoiResult;
  23. import com.baidu.mapapi.MKSearch;
  24. import com.baidu.mapapi.MKSearchListener;
  25. import com.baidu.mapapi.MKSuggestionResult;
  26. import com.baidu.mapapi.MKTransitRouteResult;
  27. import com.baidu.mapapi.MKWalkingRouteResult;
  28. import com.baidu.mapapi.MapActivity;
  29. import com.baidu.mapapi.MapController;
  30. import com.baidu.mapapi.MapView;
  31. import com.baidu.mapapi.Overlay;
  32. public class LocationActivity extends MapActivity implements LocationListener {
  33. private MapView mapView;
  34. private MapController mMapCtrl;
  35. private List<Overlay> mapOverlays;
  36. public GeoPoint locPoint;
  37. private MyMapOverlay mOverlay;
  38. private TextView desText;
  39. private String lost_tips;
  40. private int point_X;
  41. private int point_Y;
  42. public final int MSG_VIEW_LONGPRESS = 10001;
  43. public final int MSG_VIEW_ADDRESSNAME = 10002;
  44. public final int MSG_GONE_ADDRESSNAME = 10003;
  45. private Intent mIntent;
  46. private int mLatitude;
  47. private int mLongitude;
  48. private String name;
  49. private BMapManager mapManager;
  50. private MKLocationManager mLocationManager = null;
  51. private boolean isLoadAdrr = true;
  52. private MKSearch mMKSearch;
  53. @Override
  54. public void onCreate(Bundle savedInstanceState) {
  55. super.onCreate(savedInstanceState);
  56. requestWindowFeature(Window.FEATURE_NO_TITLE);
  57. setContentView(R.layout.main);
  58. initMap();
  59. mIntent = getIntent();
  60. mLatitude = mIntent.getIntExtra("latitude", 0);
  61. mLongitude = mIntent.getIntExtra("longitude", 0);
  62. name = mIntent.getStringExtra("name");
  63. mapView = (MapView) findViewById(R.id.map_view);
  64. desText = (TextView) this.findViewById(R.id.map_bubbleText);
  65. lost_tips = getResources().getString(R.string.load_tips);
  66. if (mLatitude != 0 && mLongitude != 0) {
  67. locPoint = new GeoPoint((int) (mLatitude * 1E6),
  68. (int) (mLongitude * 1E6));
  69. desText.setText(name);
  70. }
  71. mapView.setBuiltInZoomControls(true);
  72. mapView.setClickable(true);
  73. mMapCtrl = mapView.getController();
  74. point_X = this.getWindowManager().getDefaultDisplay().getWidth() / 2;
  75. point_Y = this.getWindowManager().getDefaultDisplay().getHeight() / 2;
  76. mOverlay = new MyMapOverlay(point_X, point_Y) {
  77. @Override
  78. public void changePoint(GeoPoint newPoint, int type) {
  79. if (type == 1) {
  80. mHandler.sendEmptyMessage(MSG_GONE_ADDRESSNAME);
  81. } else {
  82. locPoint = newPoint;
  83. mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);
  84. }
  85. }
  86. };
  87. mapOverlays = mapView.getOverlays();
  88. if (mapOverlays.size() > 0) {
  89. mapOverlays.clear();
  90. }
  91. mapOverlays.add(mOverlay);
  92. mMapCtrl.setZoom(20);
  93. }
  94. private void initMap() {
  95. // 初始化MapActivity
  96. mapManager = new BMapManager(getApplication());
  97. // init方法的第一个参数需填入申请的API Key
  98. mapManager.init("C66C0501D0280744759A6957C42543AE38F5D540", null);
  99. super.initMapActivity(mapManager);
  100. // 实例化搜索地址类
  101. mMKSearch = new MKSearch();
  102. // 初始化搜索地址实例
  103. mMKSearch.init(mapManager, new MySearchListener());
  104. mLocationManager = mapManager.getLocationManager();
  105. // 注册位置更新事件
  106. mLocationManager.requestLocationUpdates(this);
  107. // 使用GPS定位
  108. mLocationManager
  109. .enableProvider((int) MKLocationManager.MK_GPS_PROVIDER);
  110. }
  111. @Override
  112. protected void onResume() {
  113. if (mapManager != null) {
  114. mapManager.start();
  115. }
  116. super.onResume();
  117. }
  118. @Override
  119. protected void onPause() {
  120. isLoadAdrr = false;
  121. if (mapManager != null) {
  122. mapManager.stop();
  123. }
  124. super.onPause();
  125. }
  126. @Override
  127. protected boolean isRouteDisplayed() {
  128. // TODO Auto-generated method stub
  129. return false;
  130. }
  131. /**
  132. * 通过经纬度获取地址
  133. *
  134. * @param point
  135. * @return
  136. */
  137. private String getLocationAddress(GeoPoint point) {
  138. String add = "";
  139. Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
  140. try {
  141. List<Address> addresses = geoCoder.getFromLocation(
  142. point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6,
  143. 1);
  144. Address address = addresses.get(0);
  145. int maxLine = address.getMaxAddressLineIndex();
  146. if (maxLine >= 2) {
  147. add = address.getAddressLine(1) + address.getAddressLine(2);
  148. } else {
  149. add = address.getAddressLine(1);
  150. }
  151. } catch (IOException e) {
  152. add = "";
  153. e.printStackTrace();
  154. }
  155. return add;
  156. }
  157. private Handler mHandler = new Handler() {
  158. @Override
  159. public void handleMessage(Message msg) {
  160. switch (msg.what) {
  161. case MSG_VIEW_LONGPRESS:// 处理长按时间返回位置信息
  162. {
  163. if (null == locPoint)
  164. return;
  165. mMKSearch.reverseGeocode(locPoint);
  166. desText.setVisibility(View.VISIBLE);
  167. desText.setText(lost_tips);
  168. mMapCtrl.animateTo(locPoint);
  169. mapView.invalidate();
  170. }
  171. break;
  172. case MSG_VIEW_ADDRESSNAME:
  173. desText.setText((String) msg.obj);
  174. desText.setVisibility(View.VISIBLE);
  175. break;
  176. case MSG_GONE_ADDRESSNAME:
  177. desText.setVisibility(View.GONE);
  178. break;
  179. }
  180. }
  181. };
  182. // 关闭程序也关闭定位
  183. @Override
  184. protected void onDestroy() {
  185. if (mapManager != null) {
  186. mapManager.destroy();
  187. mapManager = null;
  188. }
  189. super.onDestroy();
  190. }
  191. /**
  192. * 根据MyLocationOverlay配置的属性确定是否在地图上显示当前位置
  193. */
  194. @Override
  195. protected boolean isLocationDisplayed() {
  196. return false;
  197. }
  198. /**
  199. * 当位置发生变化时触发此方法
  200. *
  201. * @param location
  202. *            当前位置
  203. */
  204. public void onLocationChanged(Location location) {
  205. if (location != null) {
  206. locPoint = new GeoPoint((int) (location.getLatitude()* 1E6),
  207. (int) (location.getLongitude()* 1E6));
  208. mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);
  209. }
  210. }
  211. /**
  212. * 内部类实现MKSearchListener接口,用于实现异步搜索服务
  213. *
  214. * @author liufeng
  215. */
  216. public class MySearchListener implements MKSearchListener {
  217. /**
  218. * 根据经纬度搜索地址信息结果
  219. *
  220. * @param result
  221. *            搜索结果
  222. * @param iError
  223. *            错误号(0表示正确返回)
  224. */
  225. public void onGetAddrResult(MKAddrInfo result, int iError) {
  226. if (result == null) {
  227. return;
  228. }
  229. Message msg = new Message();
  230. msg.what = MSG_VIEW_ADDRESSNAME;
  231. msg.obj = result.strAddr;
  232. mHandler.sendMessage(msg);
  233. }
  234. /**
  235. * 驾车路线搜索结果
  236. *
  237. * @param result
  238. *            搜索结果
  239. * @param iError
  240. *            错误号(0表示正确返回)
  241. */
  242. public void onGetDrivingRouteResult(MKDrivingRouteResult result,
  243. int iError) {
  244. }
  245. /**
  246. * POI搜索结果(范围检索、城市POI检索、周边检索)
  247. *
  248. * @param result
  249. *            搜索结果
  250. * @param type
  251. *            返回结果类型(11,12,21:poi列表 7:城市列表)
  252. * @param iError
  253. *            错误号(0表示正确返回)
  254. */
  255. public void onGetPoiResult(MKPoiResult result, int type, int iError) {
  256. }
  257. /**
  258. * 公交换乘路线搜索结果
  259. *
  260. * @param result
  261. *            搜索结果
  262. * @param iError
  263. *            错误号(0表示正确返回)
  264. */
  265. public void onGetTransitRouteResult(MKTransitRouteResult result,
  266. int iError) {
  267. }
  268. /**
  269. * 步行路线搜索结果
  270. *
  271. * @param result
  272. *            搜索结果
  273. * @param iError
  274. *            错误号(0表示正确返回)
  275. */
  276. public void onGetWalkingRouteResult(MKWalkingRouteResult result,
  277. int iError) {
  278. }
  279. public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
  280. // TODO Auto-generated method stub
  281. }
  282. public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
  283. // TODO Auto-generated method stub
  284. }
  285. }
  286. }

五:在AndroidManifest.xml住添加相关的访问权限

<!-- 访问网络的权限 -->

  1. <uses-permission android:name="android.permission.INTERNET" />
  2. <!-- 访问精确位置的权限 -->
  3. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  4. <!-- 访问网络状态的权限 -->
  5. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  6. <!-- 访问WIFI网络状态的权限 -->
  7. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  8. <!-- 改变WIFI网络状态的权限 -->
  9. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  10. <!-- 读写存储卡的权限 -->
  11. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  12. <!-- 读取电话状态的权限 -->
  13. <uses-permission android:name="android.permission.READ_PHONE_STATE" />

六:运行结果如下图:

最新文章

  1. Windows动态库学习心得
  2. Yocto开发笔记之《嵌入式linux libcurl编程》(QQ交流群:519230208)
  3. CENTOS7 添加自定义快捷键(启动TERMINAL,显示桌面等)
  4. 传智168期JavaEE就业班 day01-html
  5. Duilib实现GroupBox控件
  6. oracle遇到死锁杀进程
  7. C# XML - XmlNode and XmlAttribute
  8. maven 的各种命令
  9. Arcgis API for Android之GPS定位
  10. SharePoint数据视图无法打开
  11. PHP判断一个变量是否可以通过foreach进行遍历
  12. mysql字符集问题
  13. oracle之备份详解
  14. Python之路【第四篇】:Python基础之函数
  15. tiny4412 --Uboot移植(4) 串口
  16. 洛谷P1399 快餐店
  17. 第一次使用zxxbox弹层经历
  18. 关于daterangepicker的配置
  19. iOS:WKWebView(19-01-31更)
  20. Python3 Tkinter-OptionMenu

热门文章

  1. SharePoint Security and Permission System Overview
  2. AM335x(TQ335x)学习笔记——挂载Ramdisk
  3. ORACLE的sign函数和DECODE函数
  4. 刷票 变 IP
  5. (原创)win7自带IIS7.5+php7.0.10安装教程(图)
  6. 京东拍拍网 笔试 搞java的去考C++ 苦逼
  7. 动态代理CGlib实例
  8. poj 1741 Tree(点分治)
  9. bzoj 1034 [ZJOI2008]泡泡堂BNB(贪心)
  10. linux产生静态库和动态库