安卓百度地图开发(四)在定位图层显示他人位置

https://blog.csdn.net/yyyerica/article/details/80457639

在这篇博客中主要做了两个工作

① 添加显示他人位置的marker

② 将他人头像作为图片合并到marker中进行显示


主要代码:

① 添加他人位置

这段代码可以添加到构造函数中,或者从服务器获取到他人位置后的代码中去


  
  
  1. //添加他人位置
  2. Resources r = this.getResources();
  3. Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.touxiang);
  4. addOthersLocation( double(对方的经度), double(对方的纬度),bmp(对方的头像bitmap));

主要函数:


  
  
  1. public void addOthersLocation(double latitute,double longtitute, Bitmap touxiang) {
  2. Resources r = this.getResources();
  3. Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.icon_geo); //红点
  4. // //构建Marker图标
  5. // othersCurrentMarker = BitmapDescriptorFactory
  6. // .fromResource(R.drawable.icon_geo);
  7. //构建Marker图标
  8. othersCurrentMarker = BitmapDescriptorFactory
  9. .fromBitmap(mergeBitmap(bmp,touxiang)); //public static BitmapDescriptor fromBitmap(Bitmap image) + mergeBitmap()
  10. //定义Maker坐标点
  11. LatLng point = new LatLng(latitute, longtitute);
  12. //构建MarkerOption,用于在地图上添加Marker
  13. OverlayOptions option = new MarkerOptions()
  14. .position(point)
  15. .icon(othersCurrentMarker);
  16. //在地图上添加Marker,并显示
  17. mBaiduMap.addOverlay(option);
  18. }

② 合并标志点图片和用户头像图像


  
  
  1. //将两张图片合并为一张图片 用作头像
  2. private Bitmap mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {
  3. int Width = firstBitmap.getWidth();
  4. int height = firstBitmap.getHeight();
  5. secondBitmap = zoomImage(secondBitmap,Width,height);
  6. Bitmap bitmap = Bitmap.createBitmap(Width, height* 2,
  7. firstBitmap.getConfig());
  8. Canvas canvas = new Canvas(bitmap);
  9. canvas.drawBitmap(secondBitmap, new Matrix(), );
  10. canvas.drawBitmap(firstBitmap, 0, height, );
  11. return bitmap;
  12. }
  13. //缩放头像图片
  14. public Bitmap zoomImage(Bitmap bgimage, double newWidth,
  15. double newHeight) {
  16. // 获取这个图片的宽和高
  17. float width = bgimage.getWidth();
  18. float height = bgimage.getHeight();
  19. // 创建操作图片用的matrix对象
  20. Matrix matrix = new Matrix();
  21. // 计算宽高缩放率
  22. float scaleWidth = (( float) newWidth) / width;
  23. float scaleHeight = (( float) newHeight) / height;
  24. // 缩放图片动作
  25. matrix.postScale(scaleWidth, scaleHeight);
  26. Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, ( int) width,
  27. ( int) height, matrix, true);
  28. return bitmap;
  29. }



完整代码:


  
  
  1. import android.Manifest;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.pm.PackageManager;
  5. import android.content.res.Resources;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.graphics.Canvas;
  9. import android.graphics.Matrix;
  10. import android.graphics.drawable.BitmapDrawable;
  11. import android.graphics.drawable.Drawable;
  12. import android.hardware.Sensor;
  13. import android.hardware.SensorEvent;
  14. import android.hardware.SensorEventListener;
  15. import android.hardware.SensorManager;
  16. import android.os.Build;
  17. import android.os.Bundle;
  18. import android.support.v4.app.ActivityCompat;
  19. import android.support.v4.content.ContextCompat;
  20. import android.util.Log;
  21. import android.view.View;
  22. import android.view.View.OnClickListener;
  23. import android.widget.Button;
  24. import android.widget.CompoundButton;
  25. import android.widget.ImageView;
  26. import android.widget.RadioGroup.OnCheckedChangeListener;
  27. import android.widget.Toast;
  28. import android.widget.ToggleButton;
  29. import android.widget.ZoomControls;
  30. import com.baidu.location.BDLocation;
  31. import com.baidu.location.BDLocationListener;
  32. import com.baidu.location.LocationClient;
  33. import com.baidu.location.LocationClientOption;
  34. import com.baidu.mapapi.SDKInitializer;
  35. import com.baidu.mapapi.map.BaiduMap;
  36. import com.baidu.mapapi.map.BitmapDescriptor;
  37. import com.baidu.mapapi.map.BitmapDescriptorFactory;
  38. import com.baidu.mapapi.map.MapStatusUpdate;
  39. import com.baidu.mapapi.map.MapStatusUpdateFactory;
  40. import com.baidu.mapapi.map.MapView;
  41. import com.baidu.mapapi.map.MarkerOptions;
  42. import com.baidu.mapapi.map.MyLocationConfiguration;
  43. import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
  44. import com.baidu.mapapi.map.MyLocationData;
  45. import com.baidu.mapapi.map.OverlayOptions;
  46. import com.baidu.mapapi.model.LatLng;
  47. /**
  48. * 此demo用来展示如何结合定位SDK实现定位,并使用MyLocationOverlay绘制定位位置 同时展示如何使用自定义图标绘制并点击时弹出泡泡
  49. *
  50. */
  51. public class LocationDemo extends Activity {
  52. // 定位相关
  53. LocationClient mLocClient;
  54. public MyLocationListenner myListener = new MyLocationListenner();
  55. private LocationMode mCurrentMode;
  56. BitmapDescriptor mCurrentMarker,othersCurrentMarker;
  57. MapView mMapView;
  58. BaiduMap mBaiduMap;
  59. private SensorManager mSensorManager;
  60. double degree = 0;
  61. // UI相关
  62. OnCheckedChangeListener radioButtonListener;
  63. Button requestLocButton;
  64. ToggleButton togglebtn = ;
  65. boolean isFirstLoc = true; // 是否首次定位
  66. @Override
  67. public void onCreate(Bundle savedInstanceState) {
  68. super.onCreate(savedInstanceState);
  69. SDKInitializer.initialize(getApplicationContext());
  70. setContentView(R.layout.activity_location);
  71. judgePermission();
  72. requestLocButton = (Button) findViewById(R.id.button1);
  73. mCurrentMode = LocationMode.NORMAL;
  74. requestLocButton.setText( "普通");
  75. OnClickListener btnClickListener = new OnClickListener() {
  76. public void onClick(View v) {
  77. switch (mCurrentMode) {
  78. case NORMAL:
  79. requestLocButton.setText( "跟随");
  80. mCurrentMode = LocationMode.FOLLOWING;
  81. mBaiduMap
  82. .setMyLocationConfigeration( new MyLocationConfiguration(
  83. mCurrentMode, true, mCurrentMarker));
  84. break;
  85. case COMPASS:
  86. requestLocButton.setText( "普通");
  87. mCurrentMode = LocationMode.NORMAL;
  88. mBaiduMap
  89. .setMyLocationConfigeration( new MyLocationConfiguration(
  90. mCurrentMode, true, mCurrentMarker));
  91. break;
  92. case FOLLOWING:
  93. requestLocButton.setText( "罗盘");
  94. mCurrentMode = LocationMode.COMPASS;
  95. mBaiduMap
  96. .setMyLocationConfigeration( new MyLocationConfiguration(
  97. mCurrentMode, true, mCurrentMarker));
  98. break;
  99. }
  100. }
  101. };
  102. requestLocButton.setOnClickListener(btnClickListener);
  103. togglebtn = (ToggleButton) findViewById(R.id.togglebutton);
  104. togglebtn
  105. .setOnCheckedChangeListener( new android.widget.CompoundButton.OnCheckedChangeListener() {
  106. @Override
  107. public void onCheckedChanged(CompoundButton buttonView,
  108. boolean isChecked) {
  109. if (isChecked) {
  110. // 普通地图
  111. mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
  112. } else {
  113. // 卫星地图
  114. mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
  115. }
  116. }
  117. });
  118. // 地图初始化
  119. mMapView = (MapView) findViewById(R.id.bmapView);
  120. mBaiduMap = mMapView.getMap();
  121. // 开启定位图层
  122. mBaiduMap.setMyLocationEnabled( true);
  123. // 定位初始化
  124. mLocClient = new LocationClient( this);
  125. mLocClient.registerLocationListener(myListener);
  126. LocationClientOption option = new LocationClientOption();
  127. option.setOpenGps( true); // 打开gps
  128. option.setCoorType( "bd09ll"); // 设置坐标类型
  129. option.setScanSpan( 1000); // 设置发起定位请求的间隔时间为1000ms
  130. option.setIsNeedAddress( true);
  131. mLocClient.setLocOption(option);
  132. mLocClient.start();
  133. // 隐藏logo
  134. View child = mMapView.getChildAt( 1);
  135. if (child != && (child instanceof ImageView || child instanceof ZoomControls)){
  136. child.setVisibility(View.INVISIBLE);
  137. }
  138. //指南针
  139. mSensorManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  140. Sensor magenticSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
  141. Sensor accelerometerSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  142. mSensorManager.registerListener(listener,magenticSensor,SensorManager.SENSOR_DELAY_GAME);
  143. mSensorManager.registerListener(listener,accelerometerSensor,SensorManager.SENSOR_DELAY_GAME);
  144. //添加他人位置
  145. Resources r = this.getResources();
  146. Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.touxiang);
  147. addOthersLocation(经度( double),纬度( double),头像bmp);
  148. }
  149. //指南针 获取degree值
  150. private SensorEventListener listener= new SensorEventListener() {
  151. float[] accelerometerValues= new float[ 3];
  152. float[] magenticValues= new float[ 3];
  153. @Override
  154. public void onSensorChanged(SensorEvent sensorEvent) {
  155. //判断当前是加速度传感器还是地磁传感器
  156. if(sensorEvent.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
  157. accelerometerValues=sensorEvent.values.clone();
  158. } else if(sensorEvent.sensor.getType()== Sensor.TYPE_MAGNETIC_FIELD){
  159. magenticValues=sensorEvent.values.clone();
  160. }
  161. float[] R= new float[ 9];
  162. float[] values= new float[ 3];
  163. SensorManager.getRotationMatrix(R, , accelerometerValues, magenticValues);
  164. SensorManager.getOrientation(R,values);
  165. Log.d( "MainActivity", "value[0] is"+Math.toDegrees(values[ 0]));
  166. degree = Math.toDegrees(values[ 0]);
  167. }
  168. @Override
  169. public void onAccuracyChanged(Sensor sensor, int i) {
  170. }
  171. };
  172. public void addOthersLocation(double latitute,double longtitute, Bitmap touxiang) {
  173. Resources r = this.getResources();
  174. Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.icon_geo); //标识红点
  175. // //构建Marker图标
  176. // othersCurrentMarker = BitmapDescriptorFactory
  177. // .fromResource(R.drawable.icon_geo);
  178. //构建Marker图标
  179. othersCurrentMarker = BitmapDescriptorFactory
  180. .fromBitmap(mergeBitmap(bmp,touxiang)); //public static BitmapDescriptor fromBitmap(Bitmap image) + mergeBitmap()
  181. //定义Maker坐标点
  182. LatLng point = new LatLng(latitute, longtitute);
  183. //构建MarkerOption,用于在地图上添加Marker
  184. OverlayOptions option = new MarkerOptions()
  185. .position(point)
  186. .icon(othersCurrentMarker);
  187. //在地图上添加Marker,并显示
  188. mBaiduMap.addOverlay(option);
  189. }
  190. //将两张图片合并为一张图片 用作头像
  191. private Bitmap mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {
  192. int Width = firstBitmap.getWidth();
  193. int height = firstBitmap.getHeight();
  194. secondBitmap = zoomImage(secondBitmap,Width,height);
  195. Bitmap bitmap = Bitmap.createBitmap(Width, height* 2,
  196. firstBitmap.getConfig());
  197. Canvas canvas = new Canvas(bitmap);
  198. canvas.drawBitmap(secondBitmap, new Matrix(), );
  199. canvas.drawBitmap(firstBitmap, 0, height, );
  200. return bitmap;
  201. }
  202. //缩放头像图片
  203. public Bitmap zoomImage(Bitmap bgimage, double newWidth,
  204. double newHeight) {
  205. // 获取这个图片的宽和高
  206. float width = bgimage.getWidth();
  207. float height = bgimage.getHeight();
  208. // 创建操作图片用的matrix对象
  209. Matrix matrix = new Matrix();
  210. // 计算宽高缩放率
  211. float scaleWidth = (( float) newWidth) / width;
  212. float scaleHeight = (( float) newHeight) / height;
  213. // 缩放图片动作
  214. matrix.postScale(scaleWidth, scaleHeight);
  215. Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, ( int) width,
  216. ( int) height, matrix, true);
  217. return bitmap;
  218. }
  219. /**
  220. * 定位SDK监听函数
  221. */
  222. public class MyLocationListenner implements BDLocationListener {
  223. @Override
  224. public void onReceiveLocation(BDLocation location) {
  225. // map view 销毁后不在处理新接收的位置
  226. if (location == || mMapView == )
  227. return;
  228. MyLocationData locData = new MyLocationData.Builder()
  229. .accuracy(location.getRadius())
  230. // 此处设置开发者获取到的方向信息,顺时针0-360
  231. .direction(( float)degree).latitude(location.getLatitude())
  232. .longitude(location.getLongitude()).build();
  233. mBaiduMap.setMyLocationData(locData);
  234. if (isFirstLoc) {
  235. isFirstLoc = false;
  236. //定义Maker坐标点 https://blog.csdn.net/u012382791/article/details/53856203
  237. LatLng ll = new LatLng(location.getLatitude(),
  238. location.getLongitude());
  239. // MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
  240. // 设置缩放比例,更新地图状态
  241. float f = mBaiduMap.getMaxZoomLevel(); // 19.0
  242. MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll,
  243. f - 2);
  244. mBaiduMap.animateMapStatus(u);
  245. //地图位置显示
  246. Toast.makeText(LocationDemo. this, location.getAddrStr(),
  247. Toast.LENGTH_SHORT).show();
  248. }
  249. }
  250. public void onReceivePoi(BDLocation poiLocation) {
  251. }
  252. }
  253. @Override
  254. protected void onPause() {
  255. mMapView.onPause();
  256. super.onPause();
  257. }
  258. @Override
  259. protected void onResume() {
  260. mMapView.onResume();
  261. super.onResume();
  262. }
  263. @Override
  264. protected void onDestroy() {
  265. // 退出时销毁定位
  266. mLocClient.stop();
  267. // 关闭定位图层
  268. mBaiduMap.setMyLocationEnabled( false);
  269. mMapView.onDestroy();
  270. mMapView = ;
  271. if(mSensorManager!= ){
  272. mSensorManager.unregisterListener(listener);
  273. }
  274. super.onDestroy();
  275. }
  276. //6.0之后要动态获取权限,重要!!!
  277. protected void judgePermission() {
  278. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  279. // 检查该权限是否已经获取
  280. // 权限是否已经 授权 GRANTED---授权 DINIED---拒绝
  281. // sd卡权限
  282. String[] SdCardPermission = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
  283. if (ContextCompat.checkSelfPermission( this, SdCardPermission[ 0]) != PackageManager.PERMISSION_GRANTED) {
  284. // 如果没有授予该权限,就去提示用户请求
  285. ActivityCompat.requestPermissions( this, SdCardPermission, 100);
  286. }
  287. //手机状态权限
  288. String[] readPhoneStatePermission = {Manifest.permission.READ_PHONE_STATE};
  289. if (ContextCompat.checkSelfPermission( this, readPhoneStatePermission[ 0]) != PackageManager.PERMISSION_GRANTED) {
  290. // 如果没有授予该权限,就去提示用户请求
  291. ActivityCompat.requestPermissions( this, readPhoneStatePermission, 200);
  292. }
  293. //定位权限
  294. String[] locationPermission = {Manifest.permission.ACCESS_FINE_LOCATION};
  295. if (ContextCompat.checkSelfPermission( this, locationPermission[ 0]) != PackageManager.PERMISSION_GRANTED) {
  296. // 如果没有授予该权限,就去提示用户请求
  297. ActivityCompat.requestPermissions( this, locationPermission, 300);
  298. }
  299. String[] ACCESS_COARSE_LOCATION = {Manifest.permission.ACCESS_COARSE_LOCATION};
  300. if (ContextCompat.checkSelfPermission( this, ACCESS_COARSE_LOCATION[ 0]) != PackageManager.PERMISSION_GRANTED) {
  301. // 如果没有授予该权限,就去提示用户请求
  302. ActivityCompat.requestPermissions( this, ACCESS_COARSE_LOCATION, 400);
  303. }
  304. String[] READ_EXTERNAL_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE};
  305. if (ContextCompat.checkSelfPermission( this, READ_EXTERNAL_STORAGE[ 0]) != PackageManager.PERMISSION_GRANTED) {
  306. // 如果没有授予该权限,就去提示用户请求
  307. ActivityCompat.requestPermissions( this, READ_EXTERNAL_STORAGE, 500);
  308. }
  309. String[] WRITE_EXTERNAL_STORAGE = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
  310. if (ContextCompat.checkSelfPermission( this, WRITE_EXTERNAL_STORAGE[ 0]) != PackageManager.PERMISSION_GRANTED) {
  311. // 如果没有授予该权限,就去提示用户请求
  312. ActivityCompat.requestPermissions( this, WRITE_EXTERNAL_STORAGE, 600);
  313. }
  314. } else{
  315. //doSdCardResult();
  316. }
  317. //LocationClient.reStart();
  318. }
  319. }



效果截图:


左上角红点带头像的就是别人的位置显示

在这篇博客中主要做了两个工作

① 添加显示他人位置的marker

② 将他人头像作为图片合并到marker中进行显示


主要代码:

① 添加他人位置

这段代码可以添加到构造函数中,或者从服务器获取到他人位置后的代码中去


  
  
  1. //添加他人位置
  2. Resources r = this.getResources();
  3. Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.touxiang);
  4. addOthersLocation( double(对方的经度), double(对方的纬度),bmp(对方的头像bitmap));

主要函数:


  
  
  1. public void addOthersLocation(double latitute,double longtitute, Bitmap touxiang) {
  2. Resources r = this.getResources();
  3. Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.icon_geo); //红点
  4. // //构建Marker图标
  5. // othersCurrentMarker = BitmapDescriptorFactory
  6. // .fromResource(R.drawable.icon_geo);
  7. //构建Marker图标
  8. othersCurrentMarker = BitmapDescriptorFactory
  9. .fromBitmap(mergeBitmap(bmp,touxiang)); //public static BitmapDescriptor fromBitmap(Bitmap image) + mergeBitmap()
  10. //定义Maker坐标点
  11. LatLng point = new LatLng(latitute, longtitute);
  12. //构建MarkerOption,用于在地图上添加Marker
  13. OverlayOptions option = new MarkerOptions()
  14. .position(point)
  15. .icon(othersCurrentMarker);
  16. //在地图上添加Marker,并显示
  17. mBaiduMap.addOverlay(option);
  18. }

② 合并标志点图片和用户头像图像


  
  
  1. //将两张图片合并为一张图片 用作头像
  2. private Bitmap mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {
  3. int Width = firstBitmap.getWidth();
  4. int height = firstBitmap.getHeight();
  5. secondBitmap = zoomImage(secondBitmap,Width,height);
  6. Bitmap bitmap = Bitmap.createBitmap(Width, height* 2,
  7. firstBitmap.getConfig());
  8. Canvas canvas = new Canvas(bitmap);
  9. canvas.drawBitmap(secondBitmap, new Matrix(), );
  10. canvas.drawBitmap(firstBitmap, 0, height, );
  11. return bitmap;
  12. }
  13. //缩放头像图片
  14. public Bitmap zoomImage(Bitmap bgimage, double newWidth,
  15. double newHeight) {
  16. // 获取这个图片的宽和高
  17. float width = bgimage.getWidth();
  18. float height = bgimage.getHeight();
  19. // 创建操作图片用的matrix对象
  20. Matrix matrix = new Matrix();
  21. // 计算宽高缩放率
  22. float scaleWidth = (( float) newWidth) / width;
  23. float scaleHeight = (( float) newHeight) / height;
  24. // 缩放图片动作
  25. matrix.postScale(scaleWidth, scaleHeight);
  26. Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, ( int) width,
  27. ( int) height, matrix, true);
  28. return bitmap;
  29. }



完整代码:


  
  
  1. import android.Manifest;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.pm.PackageManager;
  5. import android.content.res.Resources;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.graphics.Canvas;
  9. import android.graphics.Matrix;
  10. import android.graphics.drawable.BitmapDrawable;
  11. import android.graphics.drawable.Drawable;
  12. import android.hardware.Sensor;
  13. import android.hardware.SensorEvent;
  14. import android.hardware.SensorEventListener;
  15. import android.hardware.SensorManager;
  16. import android.os.Build;
  17. import android.os.Bundle;
  18. import android.support.v4.app.ActivityCompat;
  19. import android.support.v4.content.ContextCompat;
  20. import android.util.Log;
  21. import android.view.View;
  22. import android.view.View.OnClickListener;
  23. import android.widget.Button;
  24. import android.widget.CompoundButton;
  25. import android.widget.ImageView;
  26. import android.widget.RadioGroup.OnCheckedChangeListener;
  27. import android.widget.Toast;
  28. import android.widget.ToggleButton;
  29. import android.widget.ZoomControls;
  30. import com.baidu.location.BDLocation;
  31. import com.baidu.location.BDLocationListener;
  32. import com.baidu.location.LocationClient;
  33. import com.baidu.location.LocationClientOption;
  34. import com.baidu.mapapi.SDKInitializer;
  35. import com.baidu.mapapi.map.BaiduMap;
  36. import com.baidu.mapapi.map.BitmapDescriptor;
  37. import com.baidu.mapapi.map.BitmapDescriptorFactory;
  38. import com.baidu.mapapi.map.MapStatusUpdate;
  39. import com.baidu.mapapi.map.MapStatusUpdateFactory;
  40. import com.baidu.mapapi.map.MapView;
  41. import com.baidu.mapapi.map.MarkerOptions;
  42. import com.baidu.mapapi.map.MyLocationConfiguration;
  43. import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
  44. import com.baidu.mapapi.map.MyLocationData;
  45. import com.baidu.mapapi.map.OverlayOptions;
  46. import com.baidu.mapapi.model.LatLng;
  47. /**
  48. * 此demo用来展示如何结合定位SDK实现定位,并使用MyLocationOverlay绘制定位位置 同时展示如何使用自定义图标绘制并点击时弹出泡泡
  49. *
  50. */
  51. public class LocationDemo extends Activity {
  52. // 定位相关
  53. LocationClient mLocClient;
  54. public MyLocationListenner myListener = new MyLocationListenner();
  55. private LocationMode mCurrentMode;
  56. BitmapDescriptor mCurrentMarker,othersCurrentMarker;
  57. MapView mMapView;
  58. BaiduMap mBaiduMap;
  59. private SensorManager mSensorManager;
  60. double degree = 0;
  61. // UI相关
  62. OnCheckedChangeListener radioButtonListener;
  63. Button requestLocButton;
  64. ToggleButton togglebtn = ;
  65. boolean isFirstLoc = true; // 是否首次定位
  66. @Override
  67. public void onCreate(Bundle savedInstanceState) {
  68. super.onCreate(savedInstanceState);
  69. SDKInitializer.initialize(getApplicationContext());
  70. setContentView(R.layout.activity_location);
  71. judgePermission();
  72. requestLocButton = (Button) findViewById(R.id.button1);
  73. mCurrentMode = LocationMode.NORMAL;
  74. requestLocButton.setText( "普通");
  75. OnClickListener btnClickListener = new OnClickListener() {
  76. public void onClick(View v) {
  77. switch (mCurrentMode) {
  78. case NORMAL:
  79. requestLocButton.setText( "跟随");
  80. mCurrentMode = LocationMode.FOLLOWING;
  81. mBaiduMap
  82. .setMyLocationConfigeration( new MyLocationConfiguration(
  83. mCurrentMode, true, mCurrentMarker));
  84. break;
  85. case COMPASS:
  86. requestLocButton.setText( "普通");
  87. mCurrentMode = LocationMode.NORMAL;
  88. mBaiduMap
  89. .setMyLocationConfigeration( new MyLocationConfiguration(
  90. mCurrentMode, true, mCurrentMarker));
  91. break;
  92. case FOLLOWING:
  93. requestLocButton.setText( "罗盘");
  94. mCurrentMode = LocationMode.COMPASS;
  95. mBaiduMap
  96. .setMyLocationConfigeration( new MyLocationConfiguration(
  97. mCurrentMode, true, mCurrentMarker));
  98. break;
  99. }
  100. }
  101. };
  102. requestLocButton.setOnClickListener(btnClickListener);
  103. togglebtn = (ToggleButton) findViewById(R.id.togglebutton);
  104. togglebtn
  105. .setOnCheckedChangeListener( new android.widget.CompoundButton.OnCheckedChangeListener() {
  106. @Override
  107. public void onCheckedChanged(CompoundButton buttonView,
  108. boolean isChecked) {
  109. if (isChecked) {
  110. // 普通地图
  111. mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
  112. } else {
  113. // 卫星地图
  114. mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
  115. }
  116. }
  117. });
  118. // 地图初始化
  119. mMapView = (MapView) findViewById(R.id.bmapView);
  120. mBaiduMap = mMapView.getMap();
  121. // 开启定位图层
  122. mBaiduMap.setMyLocationEnabled( true);
  123. // 定位初始化
  124. mLocClient = new LocationClient( this);
  125. mLocClient.registerLocationListener(myListener);
  126. LocationClientOption option = new LocationClientOption();
  127. option.setOpenGps( true); // 打开gps
  128. option.setCoorType( "bd09ll"); // 设置坐标类型
  129. option.setScanSpan( 1000); // 设置发起定位请求的间隔时间为1000ms
  130. option.setIsNeedAddress( true);
  131. mLocClient.setLocOption(option);
  132. mLocClient.start();
  133. // 隐藏logo
  134. View child = mMapView.getChildAt( 1);
  135. if (child != && (child instanceof ImageView || child instanceof ZoomControls)){
  136. child.setVisibility(View.INVISIBLE);
  137. }
  138. //指南针
  139. mSensorManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  140. Sensor magenticSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
  141. Sensor accelerometerSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  142. mSensorManager.registerListener(listener,magenticSensor,SensorManager.SENSOR_DELAY_GAME);
  143. mSensorManager.registerListener(listener,accelerometerSensor,SensorManager.SENSOR_DELAY_GAME);
  144. //添加他人位置
  145. Resources r = this.getResources();
  146. Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.touxiang);
  147. addOthersLocation(经度( double),纬度( double),头像bmp);
  148. }
  149. //指南针 获取degree值
  150. private SensorEventListener listener= new SensorEventListener() {
  151. float[] accelerometerValues= new float[ 3];
  152. float[] magenticValues= new float[ 3];
  153. @Override
  154. public void onSensorChanged(SensorEvent sensorEvent) {
  155. //判断当前是加速度传感器还是地磁传感器
  156. if(sensorEvent.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
  157. accelerometerValues=sensorEvent.values.clone();
  158. } else if(sensorEvent.sensor.getType()== Sensor.TYPE_MAGNETIC_FIELD){
  159. magenticValues=sensorEvent.values.clone();
  160. }
  161. float[] R= new float[ 9];
  162. float[] values= new float[ 3];
  163. SensorManager.getRotationMatrix(R, , accelerometerValues, magenticValues);
  164. SensorManager.getOrientation(R,values);
  165. Log.d( "MainActivity", "value[0] is"+Math.toDegrees(values[ 0]));
  166. degree = Math.toDegrees(values[ 0]);
  167. }
  168. @Override
  169. public void onAccuracyChanged(Sensor sensor, int i) {
  170. }
  171. };
  172. public void addOthersLocation(double latitute,double longtitute, Bitmap touxiang) {
  173. Resources r = this.getResources();
  174. Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.icon_geo); //标识红点
  175. // //构建Marker图标
  176. // othersCurrentMarker = BitmapDescriptorFactory
  177. // .fromResource(R.drawable.icon_geo);
  178. //构建Marker图标
  179. othersCurrentMarker = BitmapDescriptorFactory
  180. .fromBitmap(mergeBitmap(bmp,touxiang)); //public static BitmapDescriptor fromBitmap(Bitmap image) + mergeBitmap()
  181. //定义Maker坐标点
  182. LatLng point = new LatLng(latitute, longtitute);
  183. //构建MarkerOption,用于在地图上添加Marker
  184. OverlayOptions option = new MarkerOptions()
  185. .position(point)
  186. .icon(othersCurrentMarker);
  187. //在地图上添加Marker,并显示
  188. mBaiduMap.addOverlay(option);
  189. }
  190. //将两张图片合并为一张图片 用作头像
  191. private Bitmap mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {
  192. int Width = firstBitmap.getWidth();
  193. int height = firstBitmap.getHeight();
  194. secondBitmap = zoomImage(secondBitmap,Width,height);
  195. Bitmap bitmap = Bitmap.createBitmap(Width, height* 2,
  196. firstBitmap.getConfig());
  197. Canvas canvas = new Canvas(bitmap);
  198. canvas.drawBitmap(secondBitmap, new Matrix(), );
  199. canvas.drawBitmap(firstBitmap, 0, height, );
  200. return bitmap;
  201. }
  202. //缩放头像图片
  203. public Bitmap zoomImage(Bitmap bgimage, double newWidth,
  204. double newHeight) {
  205. // 获取这个图片的宽和高
  206. float width = bgimage.getWidth();
  207. float height = bgimage.getHeight();
  208. // 创建操作图片用的matrix对象
  209. Matrix matrix = new Matrix();
  210. // 计算宽高缩放率
  211. float scaleWidth = (( float) newWidth) / width;
  212. float scaleHeight = (( float) newHeight) / height;
  213. // 缩放图片动作
  214. matrix.postScale(scaleWidth, scaleHeight);
  215. Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, ( int) width,
  216. ( int) height, matrix, true);
  217. return bitmap;
  218. }
  219. /**
  220. * 定位SDK监听函数
  221. */
  222. public class MyLocationListenner implements BDLocationListener {
  223. @Override
  224. public void onReceiveLocation(BDLocation location) {
  225. // map view 销毁后不在处理新接收的位置
  226. if (location == || mMapView == )
  227. return;
  228. MyLocationData locData = new MyLocationData.Builder()
  229. .accuracy(location.getRadius())
  230. // 此处设置开发者获取到的方向信息,顺时针0-360
  231. .direction(( float)degree).latitude(location.getLatitude())
  232. .longitude(location.getLongitude()).build();
  233. mBaiduMap.setMyLocationData(locData);
  234. if (isFirstLoc) {
  235. isFirstLoc = false;
  236. //定义Maker坐标点 https://blog.csdn.net/u012382791/article/details/53856203
  237. LatLng ll = new LatLng(location.getLatitude(),
  238. location.getLongitude());
  239. // MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
  240. // 设置缩放比例,更新地图状态
  241. float f = mBaiduMap.getMaxZoomLevel(); // 19.0
  242. MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll,
  243. f - 2);
  244. mBaiduMap.animateMapStatus(u);
  245. //地图位置显示
  246. Toast.makeText(LocationDemo. this, location.getAddrStr(),
  247. Toast.LENGTH_SHORT).show();
  248. }
  249. }
  250. public void onReceivePoi(BDLocation poiLocation) {
  251. }
  252. }
  253. @Override
  254. protected void onPause() {
  255. mMapView.onPause();
  256. super.onPause();
  257. }
  258. @Override
  259. protected void onResume() {
  260. mMapView.onResume();
  261. super.onResume();
  262. }
  263. @Override
  264. protected void onDestroy() {
  265. // 退出时销毁定位
  266. mLocClient.stop();
  267. // 关闭定位图层
  268. mBaiduMap.setMyLocationEnabled( false);
  269. mMapView.onDestroy();
  270. mMapView = ;
  271. if(mSensorManager!= ){
  272. mSensorManager.unregisterListener(listener);
  273. }
  274. super.onDestroy();
  275. }
  276. //6.0之后要动态获取权限,重要!!!
  277. protected void judgePermission() {
  278. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  279. // 检查该权限是否已经获取
  280. // 权限是否已经 授权 GRANTED---授权 DINIED---拒绝
  281. // sd卡权限
  282. String[] SdCardPermission = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
  283. if (ContextCompat.checkSelfPermission( this, SdCardPermission[ 0]) != PackageManager.PERMISSION_GRANTED) {
  284. // 如果没有授予该权限,就去提示用户请求
  285. ActivityCompat.requestPermissions( this, SdCardPermission, 100);
  286. }
  287. //手机状态权限
  288. String[] readPhoneStatePermission = {Manifest.permission.READ_PHONE_STATE};
  289. if (ContextCompat.checkSelfPermission( this, readPhoneStatePermission[ 0]) != PackageManager.PERMISSION_GRANTED) {
  290. // 如果没有授予该权限,就去提示用户请求
  291. ActivityCompat.requestPermissions( this, readPhoneStatePermission, 200);
  292. }
  293. //定位权限
  294. String[] locationPermission = {Manifest.permission.ACCESS_FINE_LOCATION};
  295. if (ContextCompat.checkSelfPermission( this, locationPermission[ 0]) != PackageManager.PERMISSION_GRANTED) {
  296. // 如果没有授予该权限,就去提示用户请求
  297. ActivityCompat.requestPermissions( this, locationPermission, 300);
  298. }
  299. String[] ACCESS_COARSE_LOCATION = {Manifest.permission.ACCESS_COARSE_LOCATION};
  300. if (ContextCompat.checkSelfPermission( this, ACCESS_COARSE_LOCATION[ 0]) != PackageManager.PERMISSION_GRANTED) {
  301. // 如果没有授予该权限,就去提示用户请求
  302. ActivityCompat.requestPermissions( this, ACCESS_COARSE_LOCATION, 400);
  303. }
  304. String[] READ_EXTERNAL_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE};
  305. if (ContextCompat.checkSelfPermission( this, READ_EXTERNAL_STORAGE[ 0]) != PackageManager.PERMISSION_GRANTED) {
  306. // 如果没有授予该权限,就去提示用户请求
  307. ActivityCompat.requestPermissions( this, READ_EXTERNAL_STORAGE, 500);
  308. }
  309. String[] WRITE_EXTERNAL_STORAGE = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
  310. if (ContextCompat.checkSelfPermission( this, WRITE_EXTERNAL_STORAGE[ 0]) != PackageManager.PERMISSION_GRANTED) {
  311. // 如果没有授予该权限,就去提示用户请求
  312. ActivityCompat.requestPermissions( this, WRITE_EXTERNAL_STORAGE, 600);
  313. }
  314. } else{
  315. //doSdCardResult();
  316. }
  317. //LocationClient.reStart();
  318. }
  319. }



效果截图:


左上角红点带头像的就是别人的位置显示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值