一:百度地图开发必须要到百度开发平台android开发api下载相应的库,已经申请百度地图开发key,在这个博客里面有详细的说明和演示,(如果不懂得请看此文章)http://104zz.iteye.com/blog/1680781

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

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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

Java代码 收藏代码
  1. importandroid.view.MotionEvent;
  2. importcom.baidu.mapapi.GeoPoint;
  3. importcom.baidu.mapapi.MapView;
  4. importcom.baidu.mapapi.Overlay;
  5. //覆盖整个地图捕捉触控事件的OverLay
  6. publicabstractclassMyMapOverlayextendsOverlay{
  7. privateintpoint_X;
  8. privateintpoint_Y;
  9. privateGeoPointnewPoint;
  10. publicMyMapOverlay(intx,inty){
  11. point_X=x;
  12. point_Y=y;
  13. }
  14. booleanflagMove=false;
  15. //这里实现根据地图移动时重新获取屏幕中心点的经纬度坐标
  16. @Override
  17. publicbooleanonTouchEvent(MotionEventevent,MapViewmapView){
  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. }elseif(event.getAction()==MotionEvent.ACTION_UP){
  23. newPoint=mapView.getProjection().fromPixels(point_X,point_Y);
  24. changePoint(newPoint,2);
  25. }
  26. returnfalse;
  27. }
  28. publicabstractvoidchangePoint(GeoPointnewPoint,inttype);
  29. }

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

package com.location.activity;

Java代码 收藏代码
  1. importjava.io.IOException;
  2. importjava.util.List;
  3. importjava.util.Locale;
  4. importandroid.content.Intent;
  5. importandroid.location.Address;
  6. importandroid.location.Geocoder;
  7. importandroid.location.Location;
  8. importandroid.os.Bundle;
  9. importandroid.os.Handler;
  10. importandroid.os.Message;
  11. importandroid.view.View;
  12. importandroid.view.Window;
  13. importandroid.widget.TextView;
  14. importcom.android.map.MyMapOverlay;
  15. importcom.baidu.mapapi.BMapManager;
  16. importcom.baidu.mapapi.GeoPoint;
  17. importcom.baidu.mapapi.LocationListener;
  18. importcom.baidu.mapapi.MKAddrInfo;
  19. importcom.baidu.mapapi.MKBusLineResult;
  20. importcom.baidu.mapapi.MKDrivingRouteResult;
  21. importcom.baidu.mapapi.MKLocationManager;
  22. importcom.baidu.mapapi.MKPoiResult;
  23. importcom.baidu.mapapi.MKSearch;
  24. importcom.baidu.mapapi.MKSearchListener;
  25. importcom.baidu.mapapi.MKSuggestionResult;
  26. importcom.baidu.mapapi.MKTransitRouteResult;
  27. importcom.baidu.mapapi.MKWalkingRouteResult;
  28. importcom.baidu.mapapi.MapActivity;
  29. importcom.baidu.mapapi.MapController;
  30. importcom.baidu.mapapi.MapView;
  31. importcom.baidu.mapapi.Overlay;
  32. publicclassLocationActivityextendsMapActivityimplementsLocationListener{
  33. privateMapViewmapView;
  34. privateMapControllermMapCtrl;
  35. privateList<Overlay>mapOverlays;
  36. publicGeoPointlocPoint;
  37. privateMyMapOverlaymOverlay;
  38. privateTextViewdesText;
  39. privateStringlost_tips;
  40. privateintpoint_X;
  41. privateintpoint_Y;
  42. publicfinalintMSG_VIEW_LONGPRESS=10001;
  43. publicfinalintMSG_VIEW_ADDRESSNAME=10002;
  44. publicfinalintMSG_GONE_ADDRESSNAME=10003;
  45. privateIntentmIntent;
  46. privateintmLatitude;
  47. privateintmLongitude;
  48. privateStringname;
  49. privateBMapManagermapManager;
  50. privateMKLocationManagermLocationManager=null;
  51. privatebooleanisLoadAdrr=true;
  52. privateMKSearchmMKSearch;
  53. @Override
  54. publicvoidonCreate(BundlesavedInstanceState){
  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=newGeoPoint((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=newMyMapOverlay(point_X,point_Y){
  77. @Override
  78. publicvoidchangePoint(GeoPointnewPoint,inttype){
  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. privatevoidinitMap(){
  95. //初始化MapActivity
  96. mapManager=newBMapManager(getApplication());
  97. //init方法的第一个参数需填入申请的APIKey
  98. mapManager.init("C66C0501D0280744759A6957C42543AE38F5D540",null);
  99. super.initMapActivity(mapManager);
  100. //实例化搜索地址类
  101. mMKSearch=newMKSearch();
  102. //初始化搜索地址实例
  103. mMKSearch.init(mapManager,newMySearchListener());
  104. mLocationManager=mapManager.getLocationManager();
  105. //注册位置更新事件
  106. mLocationManager.requestLocationUpdates(this);
  107. //使用GPS定位
  108. mLocationManager
  109. .enableProvider((int)MKLocationManager.MK_GPS_PROVIDER);
  110. }
  111. @Override
  112. protectedvoidonResume(){
  113. if(mapManager!=null){
  114. mapManager.start();
  115. }
  116. super.onResume();
  117. }
  118. @Override
  119. protectedvoidonPause(){
  120. isLoadAdrr=false;
  121. if(mapManager!=null){
  122. mapManager.stop();
  123. }
  124. super.onPause();
  125. }
  126. @Override
  127. protectedbooleanisRouteDisplayed(){
  128. //TODOAuto-generatedmethodstub
  129. returnfalse;
  130. }
  131. /**
  132. *通过经纬度获取地址
  133. *
  134. *@parampoint
  135. *@return
  136. */
  137. privateStringgetLocationAddress(GeoPointpoint){
  138. Stringadd="";
  139. GeocodergeoCoder=newGeocoder(getBaseContext(),Locale.getDefault());
  140. try{
  141. List<Address>addresses=geoCoder.getFromLocation(
  142. point.getLatitudeE6()/1E6,point.getLongitudeE6()/1E6,
  143. 1);
  144. Addressaddress=addresses.get(0);
  145. intmaxLine=address.getMaxAddressLineIndex();
  146. if(maxLine>=2){
  147. add=address.getAddressLine(1)+address.getAddressLine(2);
  148. }else{
  149. add=address.getAddressLine(1);
  150. }
  151. }catch(IOExceptione){
  152. add="";
  153. e.printStackTrace();
  154. }
  155. returnadd;
  156. }
  157. privateHandlermHandler=newHandler(){
  158. @Override
  159. publicvoidhandleMessage(Messagemsg){
  160. switch(msg.what){
  161. caseMSG_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. caseMSG_VIEW_ADDRESSNAME:
  173. desText.setText((String)msg.obj);
  174. desText.setVisibility(View.VISIBLE);
  175. break;
  176. caseMSG_GONE_ADDRESSNAME:
  177. desText.setVisibility(View.GONE);
  178. break;
  179. }
  180. }
  181. };
  182. //关闭程序也关闭定位
  183. @Override
  184. protectedvoidonDestroy(){
  185. if(mapManager!=null){
  186. mapManager.destroy();
  187. mapManager=null;
  188. }
  189. super.onDestroy();
  190. }
  191. /**
  192. *根据MyLocationOverlay配置的属性确定是否在地图上显示当前位置
  193. */
  194. @Override
  195. protectedbooleanisLocationDisplayed(){
  196. returnfalse;
  197. }
  198. /**
  199. *当位置发生变化时触发此方法
  200. *
  201. *@paramlocation
  202. *当前位置
  203. */
  204. publicvoidonLocationChanged(Locationlocation){
  205. if(location!=null){
  206. locPoint=newGeoPoint((int)(location.getLatitude()*1E6),
  207. (int)(location.getLongitude()*1E6));
  208. mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);
  209. }
  210. }
  211. /**
  212. *内部类实现MKSearchListener接口,用于实现异步搜索服务
  213. *
  214. *@authorliufeng
  215. */
  216. publicclassMySearchListenerimplementsMKSearchListener{
  217. /**
  218. *根据经纬度搜索地址信息结果
  219. *
  220. *@paramresult
  221. *搜索结果
  222. *@paramiError
  223. *错误号(0表示正确返回)
  224. */
  225. publicvoidonGetAddrResult(MKAddrInforesult,intiError){
  226. if(result==null){
  227. return;
  228. }
  229. Messagemsg=newMessage();
  230. msg.what=MSG_VIEW_ADDRESSNAME;
  231. msg.obj=result.strAddr;
  232. mHandler.sendMessage(msg);
  233. }
  234. /**
  235. *驾车路线搜索结果
  236. *
  237. *@paramresult
  238. *搜索结果
  239. *@paramiError
  240. *错误号(0表示正确返回)
  241. */
  242. publicvoidonGetDrivingRouteResult(MKDrivingRouteResultresult,
  243. intiError){
  244. }
  245. /**
  246. *POI搜索结果(范围检索、城市POI检索、周边检索)
  247. *
  248. *@paramresult
  249. *搜索结果
  250. *@paramtype
  251. *返回结果类型(11,12,21:poi列表7:城市列表)
  252. *@paramiError
  253. *错误号(0表示正确返回)
  254. */
  255. publicvoidonGetPoiResult(MKPoiResultresult,inttype,intiError){
  256. }
  257. /**
  258. *公交换乘路线搜索结果
  259. *
  260. *@paramresult
  261. *搜索结果
  262. *@paramiError
  263. *错误号(0表示正确返回)
  264. */
  265. publicvoidonGetTransitRouteResult(MKTransitRouteResultresult,
  266. intiError){
  267. }
  268. /**
  269. *步行路线搜索结果
  270. *
  271. *@paramresult
  272. *搜索结果
  273. *@paramiError
  274. *错误号(0表示正确返回)
  275. */
  276. publicvoidonGetWalkingRouteResult(MKWalkingRouteResultresult,
  277. intiError){
  278. }
  279. publicvoidonGetBusDetailResult(MKBusLineResultarg0,intarg1){
  280. //TODOAuto-generatedmethodstub
  281. }
  282. publicvoidonGetSuggestionResult(MKSuggestionResultarg0,intarg1){
  283. //TODOAuto-generatedmethodstub
  284. }
  285. }
  286. }

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

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

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

六:运行结果如下图:


android 基于百度地图api开发定位以及获取详细地址

更多相关文章

  1. Android-sharedUserId数据权限 android:sharedUserId
  2. Android Handler 异步消息处理机制 《第一行代码》
  3. [Android]权限处理
  4. Android sharedUserId数据权限
  5. Android Studio NDK开发在C代码中将Log输出到logcat上面
  6. Android 运行时权限处理(from jianshu)
  7. 22个值得收藏的android开源代码-UI篇
  8. [Android]混淆代码后生成带签名的apk
  9. 把android sdk 1.5源代码加入SDK

随机推荐

  1. 数组下标重新归序
  2. PHP注册验证判断
  3. PHP模拟用户登陆验证
  4. 使用frp实现内网穿透
  5. 【前端 · 面试 】HTTP 总结(十一)—— HTT
  6. 8月10号作业
  7. PHP数组处理初体验
  8. PHP全国快递寄件接口,1天接入四通一达,极
  9. php电子面单接口,可一次性接入全国45家主
  10. php简单实现模拟用户登陆验证