当前位置:   article > 正文

安卓零基础到进阶——Android高德地图API定位小蓝点实现_android 高德地图 onlocationchanged才能显示蓝点

android 高德地图 onlocationchanged才能显示蓝点

推荐资源站:https://zhimalier.com/

高德地图目前的定位类型有 3 种:

LOCATION_TYPE_LOCATE :只在第一次定位移动到地图中心点;

LOCATION_TYPE_MAP_FOLLOW :定位,移动到地图中心点并跟随;

LOCATION_TYPE_MAP_ROTATE :定位,移动到地图中心点,跟踪并根据面向方向旋转地图。

 

1.首先在 http://id.amap.com/ 站点注册用户名,密码(如果没有):



2. 获得高德地图服务的 APIKey ,高德地图和百度地图的 APIKey 都是相同的,数字证书的 keystore 通常保存在 ANDROID_SDK_HOME 环境变量对应的路径的 .android/ 目录下。获取 APIKey 可有两种方式:

 

     (1). 在 DOS 命令中输入如下所示(红色部分就是 keystore 的认证):

 

 

     (2).在 Eclipse 中的 Window ---------> Preferences ---------> Android -------->Build 如下图所示:

3. 登录 http://lbs.amap.com/console/ 页面获取 KEY ,如下图:

4. 得到 KEY :

 

5. 登录 http://lbs.amap.com/api/android-sdk/down/ 站点下载所需版本,如下:

6. 登录 http://lbs.amap.com/api/android-location-sdk/down/ 下载定位包,如下图:

7. 将下载的压缩包解压后放入 Eclipse 的 libs 文件下,如下图:

      (1)如果是 AndroidStudio 则把解压得到的 3 个 jar 包放入 libs 目录下,并选中 3个 jar 包,右键单击,选择                        “Add As Library” 将其设为库;

      (2)在 src/main/ 目录下新建一个 jniLibs 子目录,将解压得到的剩余文件夹复制到该目录下。

8.  在 AndroidManifest.xml 中注册相应的权限:

9.  在 <application...../> 元素内添加如下元素,红色部分为获得的 KEY:

10.  添加 service :

11.  activity_main.xml 布局如下:


   
   
  1. <?xml version= "1.0" encoding= "utf-8"?>
  2. <FrameLayout xmlns:android= "http://schemas.android.com/apk/res/android"
  3. android:layout_width= "match_parent"
  4. android:layout_height= "match_parent"
  5. android:orientation= "vertical" >
  6. <com.amap.api.maps.MapView
  7. android:id= "@+id/map"
  8. android:layout_width= "match_parent"
  9. android:layout_height= "match_parent" >
  10. </com.amap.api.maps.MapView>
  11. <TextView
  12. android:id= "@+id/location_errInfo_text"
  13. android:layout_width= "match_parent"
  14. android:layout_height= "wrap_content"
  15. android:layout_gravity= "bottom|left"
  16. android:layout_marginBottom= "10dp"
  17. android:layout_marginLeft= "10dp"
  18. android:background= "@color/red"
  19. android:textColor= "@color/darkgrey"
  20. android:text= "TextView"
  21. android:visibility= "gone"/>
  22. <RadioGroup
  23. android:id= "@+id/gps_radio_group"
  24. android:layout_width= "wrap_content"
  25. android:layout_height= "wrap_content"
  26. android:layout_gravity= "top|left"
  27. android:layout_marginLeft= "10dp"
  28. android:layout_marginTop= "10dp"
  29. android:background= "@color/grey"
  30. android:orientation= "horizontal" >
  31. <RadioButton
  32. android:id= "@+id/gps_locate_button"
  33. android:layout_width= "wrap_content"
  34. android:layout_height= "wrap_content"
  35. android:checked= "true"
  36. android:text= "@string/gpslocate"
  37. android:textColor= "@android:color/black" />
  38. <RadioButton
  39. android:id= "@+id/gps_follow_button"
  40. android:layout_width= "wrap_content"
  41. android:layout_height= "wrap_content"
  42. android:text= "@string/gpsfollow"
  43. android:textColor= "@android:color/black" />
  44. <RadioButton
  45. android:id= "@+id/gps_rotate_button"
  46. android:layout_width= "wrap_content"
  47. android:layout_height= "wrap_content"
  48. android:text= "@string/gpsrotate"
  49. android:textColor= "@android:color/black" />
  50. </RadioGroup>
  51. </FrameLayout>
  • 1

   

MainActivity.java :


 
 
  1. package com.amap.map3d.demo;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.view.Window;
  7. import android.widget.RadioGroup;
  8. import android.widget.RadioGroup.OnCheckedChangeListener;
  9. import android.widget.TextView;
  10. import com.amap.api.location.AMapLocation;
  11. import com.amap.api.location.AMapLocationClient;
  12. import com.amap.api.location.AMapLocationClientOption;
  13. import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
  14. import com.amap.api.location.AMapLocationListener;
  15. import com.amap.api.maps.AMap;
  16. import com.amap.api.maps.LocationSource;
  17. import com.amap.api.maps.MapView;
  18. import com.amap.map3d.demo.R;
  19. public class MainActivity extends Activity implements LocationSource,
  20. AMapLocationListener, OnCheckedChangeListener {
  21. private AMap aMap;
  22. private MapView mapView;
  23. // 处理定位更新
  24. private OnLocationChangedListener mListener;
  25. // 定位
  26. private AMapLocationClient mlocationClient;
  27. private AMapLocationClientOption mLocationOption;
  28. private RadioGroup mGPSModeGroup;
  29. private TextView mLocationErrText;
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示程序的标题栏
  34. setContentView(R.layout.activity_main);
  35. mapView = (MapView) findViewById(R.id.map);
  36. mapView.onCreate(savedInstanceState); // 此方法必须重写
  37. init();
  38. }
  39. /**
  40. * 初始化
  41. */
  42. private void init() {
  43. if (aMap == null) {
  44. aMap = mapView.getMap();
  45. setUpMap();
  46. }
  47. mGPSModeGroup = (RadioGroup) findViewById(R.id.gps_radio_group);
  48. mGPSModeGroup.setOnCheckedChangeListener( this);
  49. mLocationErrText = (TextView)findViewById(R.id.location_errInfo_text);
  50. mLocationErrText.setVisibility(View.GONE);
  51. }
  52. /**
  53. * 设置一些amap的属性
  54. */
  55. private void setUpMap() {
  56. aMap.setLocationSource( this); // 设置定位监听
  57. aMap.getUiSettings().setMyLocationButtonEnabled( true); // 设置默认定位按钮是否显示
  58. aMap.setMyLocationEnabled( true); // 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
  59. // 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种
  60. aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
  61. }
  62. @Override
  63. public void onCheckedChanged(RadioGroup group, int checkedId) {
  64. switch (checkedId) {
  65. case R.id.gps_locate_button:
  66. // 设置定位的类型为定位模式
  67. aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
  68. break;
  69. case R.id.gps_follow_button:
  70. // 设置定位的类型为 跟随模式
  71. aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_FOLLOW);
  72. break;
  73. case R.id.gps_rotate_button:
  74. // 设置定位的类型为根据地图面向方向旋转
  75. aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_ROTATE);
  76. break;
  77. }
  78. }
  79. /**
  80. * 方法必须重写
  81. */
  82. @Override
  83. protected void onResume() {
  84. super.onResume();
  85. mapView.onResume();
  86. }
  87. /**
  88. * 方法必须重写
  89. */
  90. @Override
  91. protected void onPause() {
  92. super.onPause();
  93. mapView.onPause();
  94. deactivate();
  95. }
  96. /**
  97. * 方法必须重写
  98. */
  99. @Override
  100. protected void onSaveInstanceState(Bundle outState) {
  101. super.onSaveInstanceState(outState);
  102. mapView.onSaveInstanceState(outState);
  103. }
  104. /**
  105. * 方法必须重写
  106. */
  107. @Override
  108. protected void onDestroy() {
  109. super.onDestroy();
  110. mapView.onDestroy();
  111. if( null != mlocationClient){
  112. mlocationClient.onDestroy();
  113. }
  114. }
  115. /**
  116. * 定位成功后回调函数
  117. */
  118. @Override
  119. public void onLocationChanged(AMapLocation amapLocation) {
  120. if (mListener != null && amapLocation != null) {
  121. if (amapLocation != null
  122. && amapLocation.getErrorCode() == 0) {
  123. mLocationErrText.setVisibility(View.GONE);
  124. mListener.onLocationChanged(amapLocation); // 显示系统小蓝点
  125. } else {
  126. String errText = "定位失败," + amapLocation.getErrorCode()+ ": " + amapLocation.getErrorInfo();
  127. Log.e( "AmapErr",errText);
  128. mLocationErrText.setVisibility(View.VISIBLE);
  129. mLocationErrText.setText(errText);
  130. }
  131. }
  132. }
  133. /**
  134. * 激活定位
  135. */
  136. @Override
  137. public void activate(OnLocationChangedListener listener) {
  138. mListener = listener;
  139. if (mlocationClient == null) {
  140. mlocationClient = new AMapLocationClient( this);
  141. mLocationOption = new AMapLocationClientOption();
  142. //设置定位监听
  143. mlocationClient.setLocationListener( this);
  144. //设置为高精度定位模式
  145. mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
  146. //设置定位参数
  147. mlocationClient.setLocationOption(mLocationOption);
  148. // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
  149. // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
  150. // 在定位结束后,在合适的生命周期调用onDestroy()方法
  151. // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
  152. mlocationClient.startLocation();
  153. }
  154. }
  155. /**
  156. * 停止定位
  157. */
  158. @Override
  159. public void deactivate() {
  160. mListener = null;
  161. if (mlocationClient != null) {
  162. mlocationClient.stopLocation();
  163. mlocationClient.onDestroy();
  164. }
  165. mlocationClient = null;
  166. }
  167. }
  • 1

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/844673
推荐阅读
相关标签
  

闽ICP备14008679号