第一步,先要导入百度API的架包libBMapApiEngine_v1_3_3.so和baidumapapi.jar,且libBMapApiEngine_v1_3_3.so包要放到armeabi文件夹下,armeabi和baidumapapi.jar平级。 


(附件中的libBMapApiEngine_v1_3_3.zip要解压,解压后把libBMapApiEngine_v1_3_3.so包要放到armeabi文件夹下就行了) 



第二步,在布局页面里要加上如下代码 




Java代码  


1. <?xml versinotallow="1.0" encoding="utf-8"?>  
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. "fill_parent"
4. "fill_parent"
5. "vertical"
6.      <!-- 使用百度API定位,必须要定义MapView,但又不需要显示 -->  
7.     <com.baidu.mapapi.MapView   
8. "@+id/map_View"
9. "fill_parent"
10. "fill_parent"
11. "true"
12. "gone"
13.     />  
14.   
15.     <TextView  
16. "@+id/lat"
17. "fill_parent"
18. "wrap_content"
19. "纬度:"
20.       
21.     <TextView  
22. "@+id/lon"
23. "fill_parent"
24. "wrap_content"
25. "经度:"
26. </LinearLayout>


第三步:写代码




Java代码  


1. import
2. import
3. import
4.   
5. import
6. import
7. import
8. import
9.   
10. /**
11.  * 创建Activity(继承com.baidu.mapapi.MapActivity)
12.  */
13. public class LocationActivity extends MapActivity implements
14. private
15. private MKLocationManager mLocationManager = null;  
16.   
17. private
18. private
19.   
20. @Override
21. public void
22. super.onCreate(savedInstanceState);  
23.         setContentView(R.layout.main);  
24.   
25.         latText = (TextView) findViewById(R.id.lat);  
26.         lonText = (TextView) findViewById(R.id.lon);  
27.   
28. // 初始化MapActivity
29. new
30. // init方法的第一个参数需填入申请的API Key
31. "285B415EBAB2A92293E85502150ADA7F03C777C4", null);  
32. super.initMapActivity(mapManager);  
33.   
34.         mLocationManager = mapManager.getLocationManager();  
35. // 注册位置更新事件
36. this);  
37. // 使用GPS定位
38. int) MKLocationManager.MK_GPS_PROVIDER);  
39.     }  
40.   
41. @Override
42. protected boolean
43. return false;  
44.     }  
45.   
46. @Override
47. protected void
48. if (mapManager != null) {  
49.             mapManager.destroy();  
50. null;  
51.         }  
52. null;  
53. super.onDestroy();  
54.     }  
55.   
56. @Override
57. protected void
58. if (mapManager != null) {  
59.             mapManager.stop();  
60.         }  
61. super.onPause();  
62.     }  
63.   
64. @Override
65. protected void
66. if (mapManager != null) {  
67.             mapManager.start();  
68.         }  
69. super.onResume();  
70.     }  
71.   
72. /**
73.      * 根据MyLocationOverlay配置的属性确定是否在地图上显示当前位置
74.      */
75. @Override
76. protected boolean
77. return false;  
78.     }  
79.   
80. /**
81.      * 当位置发生变化时触发此方法
82.      * 
83.      * @param location 当前位置
84.      */
85. @Override
86. public void
87. if (location != null) {  
88. // 显示定位结果
89. "当前经度:"
90. "当前纬度:"
91.         }  
92.     }  
93. }

最后:添加权限 




Java代码  


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



阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: android