https://blog.csdn.net/yyyerica/article/details/80457639
在这篇博客中主要做了两个工作
① 添加显示他人位置的marker
② 将他人头像作为图片合并到marker中进行显示
主要代码:
① 添加他人位置
这段代码可以添加到构造函数中,或者从服务器获取到他人位置后的代码中去
-
//添加他人位置
-
Resources r =
this.getResources();
-
Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.touxiang);
-
addOthersLocation(
double(对方的经度),
double(对方的纬度),bmp(对方的头像bitmap));
主要函数:
-
public void addOthersLocation(double latitute,double longtitute, Bitmap touxiang) {
-
-
Resources r =
this.getResources();
-
Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.icon_geo);
//红点
-
-
// //构建Marker图标
-
// othersCurrentMarker = BitmapDescriptorFactory
-
// .fromResource(R.drawable.icon_geo);
-
-
//构建Marker图标
-
othersCurrentMarker = BitmapDescriptorFactory
-
.fromBitmap(mergeBitmap(bmp,touxiang));
//public static BitmapDescriptor fromBitmap(Bitmap image) + mergeBitmap()
-
-
//定义Maker坐标点
-
LatLng point =
new LatLng(latitute, longtitute);
-
//构建MarkerOption,用于在地图上添加Marker
-
OverlayOptions option =
new MarkerOptions()
-
.position(point)
-
.icon(othersCurrentMarker);
-
//在地图上添加Marker,并显示
-
mBaiduMap.addOverlay(option);
-
}
② 合并标志点图片和用户头像图像
-
//将两张图片合并为一张图片 用作头像
-
private Bitmap mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {
-
int Width = firstBitmap.getWidth();
-
int height = firstBitmap.getHeight();
-
secondBitmap = zoomImage(secondBitmap,Width,height);
-
Bitmap bitmap = Bitmap.createBitmap(Width, height*
2,
-
firstBitmap.getConfig());
-
Canvas canvas =
new Canvas(bitmap);
-
canvas.drawBitmap(secondBitmap,
new Matrix(),
);
-
canvas.drawBitmap(firstBitmap,
0, height,
);
-
return bitmap;
-
}
-
-
//缩放头像图片
-
public Bitmap zoomImage(Bitmap bgimage, double newWidth,
-
double newHeight) {
-
// 获取这个图片的宽和高
-
float width = bgimage.getWidth();
-
float height = bgimage.getHeight();
-
// 创建操作图片用的matrix对象
-
Matrix matrix =
new Matrix();
-
// 计算宽高缩放率
-
float scaleWidth = ((
float) newWidth) / width;
-
float scaleHeight = ((
float) newHeight) / height;
-
// 缩放图片动作
-
matrix.postScale(scaleWidth, scaleHeight);
-
Bitmap bitmap = Bitmap.createBitmap(bgimage,
0,
0, (
int) width,
-
(
int) height, matrix,
true);
-
return bitmap;
-
}
完整代码:
-
import android.Manifest;
-
import android.app.Activity;
-
import android.content.Context;
-
import android.content.pm.PackageManager;
-
import android.content.res.Resources;
-
import android.graphics.Bitmap;
-
import android.graphics.BitmapFactory;
-
import android.graphics.Canvas;
-
import android.graphics.Matrix;
-
import android.graphics.drawable.BitmapDrawable;
-
import android.graphics.drawable.Drawable;
-
import android.hardware.Sensor;
-
import android.hardware.SensorEvent;
-
import android.hardware.SensorEventListener;
-
import android.hardware.SensorManager;
-
import android.os.Build;
-
import android.os.Bundle;
-
import android.support.v4.app.ActivityCompat;
-
import android.support.v4.content.ContextCompat;
-
import android.util.Log;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
import android.widget.CompoundButton;
-
import android.widget.ImageView;
-
import android.widget.RadioGroup.OnCheckedChangeListener;
-
import android.widget.Toast;
-
import android.widget.ToggleButton;
-
import android.widget.ZoomControls;
-
-
import com.baidu.location.BDLocation;
-
import com.baidu.location.BDLocationListener;
-
import com.baidu.location.LocationClient;
-
import com.baidu.location.LocationClientOption;
-
import com.baidu.mapapi.SDKInitializer;
-
import com.baidu.mapapi.map.BaiduMap;
-
import com.baidu.mapapi.map.BitmapDescriptor;
-
import com.baidu.mapapi.map.BitmapDescriptorFactory;
-
import com.baidu.mapapi.map.MapStatusUpdate;
-
import com.baidu.mapapi.map.MapStatusUpdateFactory;
-
import com.baidu.mapapi.map.MapView;
-
import com.baidu.mapapi.map.MarkerOptions;
-
import com.baidu.mapapi.map.MyLocationConfiguration;
-
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
-
import com.baidu.mapapi.map.MyLocationData;
-
import com.baidu.mapapi.map.OverlayOptions;
-
import com.baidu.mapapi.model.LatLng;
-
-
/**
-
* 此demo用来展示如何结合定位SDK实现定位,并使用MyLocationOverlay绘制定位位置 同时展示如何使用自定义图标绘制并点击时弹出泡泡
-
*
-
*/
-
public
class LocationDemo extends Activity {
-
-
// 定位相关
-
LocationClient mLocClient;
-
public MyLocationListenner myListener =
new MyLocationListenner();
-
private LocationMode mCurrentMode;
-
BitmapDescriptor mCurrentMarker,othersCurrentMarker;
-
-
MapView mMapView;
-
BaiduMap mBaiduMap;
-
-
private SensorManager mSensorManager;
-
double degree =
0;
-
// UI相关
-
OnCheckedChangeListener radioButtonListener;
-
Button requestLocButton;
-
ToggleButton togglebtn =
;
-
boolean isFirstLoc =
true;
// 是否首次定位
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
SDKInitializer.initialize(getApplicationContext());
-
setContentView(R.layout.activity_location);
-
judgePermission();
-
requestLocButton = (Button) findViewById(R.id.button1);
-
mCurrentMode = LocationMode.NORMAL;
-
requestLocButton.setText(
"普通");
-
OnClickListener btnClickListener =
new OnClickListener() {
-
public void onClick(View v) {
-
switch (mCurrentMode) {
-
case NORMAL:
-
requestLocButton.setText(
"跟随");
-
mCurrentMode = LocationMode.FOLLOWING;
-
mBaiduMap
-
.setMyLocationConfigeration(
new MyLocationConfiguration(
-
mCurrentMode,
true, mCurrentMarker));
-
break;
-
case COMPASS:
-
requestLocButton.setText(
"普通");
-
mCurrentMode = LocationMode.NORMAL;
-
mBaiduMap
-
.setMyLocationConfigeration(
new MyLocationConfiguration(
-
mCurrentMode,
true, mCurrentMarker));
-
break;
-
case FOLLOWING:
-
requestLocButton.setText(
"罗盘");
-
mCurrentMode = LocationMode.COMPASS;
-
mBaiduMap
-
.setMyLocationConfigeration(
new MyLocationConfiguration(
-
mCurrentMode,
true, mCurrentMarker));
-
break;
-
}
-
}
-
};
-
requestLocButton.setOnClickListener(btnClickListener);
-
-
togglebtn = (ToggleButton) findViewById(R.id.togglebutton);
-
togglebtn
-
.setOnCheckedChangeListener(
new android.widget.CompoundButton.OnCheckedChangeListener() {
-
-
@Override
-
public void onCheckedChanged(CompoundButton buttonView,
-
boolean isChecked) {
-
if (isChecked) {
-
// 普通地图
-
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
-
}
else {
-
// 卫星地图
-
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
-
}
-
-
}
-
});
-
-
// 地图初始化
-
mMapView = (MapView) findViewById(R.id.bmapView);
-
mBaiduMap = mMapView.getMap();
-
// 开启定位图层
-
mBaiduMap.setMyLocationEnabled(
true);
-
// 定位初始化
-
mLocClient =
new LocationClient(
this);
-
mLocClient.registerLocationListener(myListener);
-
LocationClientOption option =
new LocationClientOption();
-
option.setOpenGps(
true);
// 打开gps
-
option.setCoorType(
"bd09ll");
// 设置坐标类型
-
option.setScanSpan(
1000);
// 设置发起定位请求的间隔时间为1000ms
-
option.setIsNeedAddress(
true);
-
mLocClient.setLocOption(option);
-
mLocClient.start();
-
// 隐藏logo
-
View child = mMapView.getChildAt(
1);
-
if (child !=
&& (child
instanceof ImageView || child
instanceof ZoomControls)){
-
child.setVisibility(View.INVISIBLE);
-
}
-
-
//指南针
-
mSensorManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
-
Sensor magenticSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
-
Sensor accelerometerSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
-
mSensorManager.registerListener(listener,magenticSensor,SensorManager.SENSOR_DELAY_GAME);
-
mSensorManager.registerListener(listener,accelerometerSensor,SensorManager.SENSOR_DELAY_GAME);
-
-
//添加他人位置
-
Resources r =
this.getResources();
-
Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.touxiang);
-
addOthersLocation(经度(
double),纬度(
double),头像bmp);
-
}
-
-
//指南针 获取degree值
-
private SensorEventListener listener=
new SensorEventListener() {
-
float[] accelerometerValues=
new
float[
3];
-
float[] magenticValues=
new
float[
3];
-
@Override
-
public void onSensorChanged(SensorEvent sensorEvent) {
-
//判断当前是加速度传感器还是地磁传感器
-
if(sensorEvent.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
-
accelerometerValues=sensorEvent.values.clone();
-
}
else
if(sensorEvent.sensor.getType()== Sensor.TYPE_MAGNETIC_FIELD){
-
magenticValues=sensorEvent.values.clone();
-
}
-
float[] R=
new
float[
9];
-
float[] values=
new
float[
3];
-
SensorManager.getRotationMatrix(R,
, accelerometerValues, magenticValues);
-
SensorManager.getOrientation(R,values);
-
Log.d(
"MainActivity",
"value[0] is"+Math.toDegrees(values[
0]));
-
degree = Math.toDegrees(values[
0]);
-
-
}
-
@Override
-
public void onAccuracyChanged(Sensor sensor, int i) {
-
}
-
};
-
-
public void addOthersLocation(double latitute,double longtitute, Bitmap touxiang) {
-
-
Resources r =
this.getResources();
-
Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.icon_geo);
//标识红点
-
-
// //构建Marker图标
-
// othersCurrentMarker = BitmapDescriptorFactory
-
// .fromResource(R.drawable.icon_geo);
-
-
//构建Marker图标
-
othersCurrentMarker = BitmapDescriptorFactory
-
.fromBitmap(mergeBitmap(bmp,touxiang));
//public static BitmapDescriptor fromBitmap(Bitmap image) + mergeBitmap()
-
-
//定义Maker坐标点
-
LatLng point =
new LatLng(latitute, longtitute);
-
//构建MarkerOption,用于在地图上添加Marker
-
OverlayOptions option =
new MarkerOptions()
-
.position(point)
-
.icon(othersCurrentMarker);
-
//在地图上添加Marker,并显示
-
mBaiduMap.addOverlay(option);
-
}
-
-
//将两张图片合并为一张图片 用作头像
-
private Bitmap mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {
-
int Width = firstBitmap.getWidth();
-
int height = firstBitmap.getHeight();
-
secondBitmap = zoomImage(secondBitmap,Width,height);
-
Bitmap bitmap = Bitmap.createBitmap(Width, height*
2,
-
firstBitmap.getConfig());
-
Canvas canvas =
new Canvas(bitmap);
-
canvas.drawBitmap(secondBitmap,
new Matrix(),
);
-
canvas.drawBitmap(firstBitmap,
0, height,
);
-
return bitmap;
-
}
-
-
//缩放头像图片
-
public Bitmap zoomImage(Bitmap bgimage, double newWidth,
-
double newHeight) {
-
// 获取这个图片的宽和高
-
float width = bgimage.getWidth();
-
float height = bgimage.getHeight();
-
// 创建操作图片用的matrix对象
-
Matrix matrix =
new Matrix();
-
// 计算宽高缩放率
-
float scaleWidth = ((
float) newWidth) / width;
-
float scaleHeight = ((
float) newHeight) / height;
-
// 缩放图片动作
-
matrix.postScale(scaleWidth, scaleHeight);
-
Bitmap bitmap = Bitmap.createBitmap(bgimage,
0,
0, (
int) width,
-
(
int) height, matrix,
true);
-
return bitmap;
-
}
-
-
/**
-
* 定位SDK监听函数
-
*/
-
public
class MyLocationListenner implements BDLocationListener {
-
-
@Override
-
public void onReceiveLocation(BDLocation location) {
-
// map view 销毁后不在处理新接收的位置
-
if (location ==
|| mMapView ==
)
-
return;
-
MyLocationData locData =
new MyLocationData.Builder()
-
.accuracy(location.getRadius())
-
// 此处设置开发者获取到的方向信息,顺时针0-360
-
.direction((
float)degree).latitude(location.getLatitude())
-
.longitude(location.getLongitude()).build();
-
mBaiduMap.setMyLocationData(locData);
-
if (isFirstLoc) {
-
isFirstLoc =
false;
-
//定义Maker坐标点 https://blog.csdn.net/u012382791/article/details/53856203
-
LatLng ll =
new LatLng(location.getLatitude(),
-
location.getLongitude());
-
// MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
-
// 设置缩放比例,更新地图状态
-
float f = mBaiduMap.getMaxZoomLevel();
// 19.0
-
MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll,
-
f -
2);
-
mBaiduMap.animateMapStatus(u);
-
//地图位置显示
-
Toast.makeText(LocationDemo.
this, location.getAddrStr(),
-
Toast.LENGTH_SHORT).show();
-
}
-
-
}
-
-
public void onReceivePoi(BDLocation poiLocation) {
-
}
-
}
-
-
@Override
-
protected void onPause() {
-
mMapView.onPause();
-
super.onPause();
-
}
-
-
@Override
-
protected void onResume() {
-
mMapView.onResume();
-
super.onResume();
-
}
-
-
@Override
-
protected void onDestroy() {
-
// 退出时销毁定位
-
mLocClient.stop();
-
// 关闭定位图层
-
mBaiduMap.setMyLocationEnabled(
false);
-
mMapView.onDestroy();
-
mMapView =
;
-
-
if(mSensorManager!=
){
-
mSensorManager.unregisterListener(listener);
-
}
-
super.onDestroy();
-
}
-
-
-
//6.0之后要动态获取权限,重要!!!
-
protected void judgePermission() {
-
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
-
// 检查该权限是否已经获取
-
// 权限是否已经 授权 GRANTED---授权 DINIED---拒绝
-
-
// sd卡权限
-
String[] SdCardPermission = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
-
if (ContextCompat.checkSelfPermission(
this, SdCardPermission[
0]) != PackageManager.PERMISSION_GRANTED) {
-
// 如果没有授予该权限,就去提示用户请求
-
ActivityCompat.requestPermissions(
this, SdCardPermission,
100);
-
}
-
-
//手机状态权限
-
String[] readPhoneStatePermission = {Manifest.permission.READ_PHONE_STATE};
-
if (ContextCompat.checkSelfPermission(
this, readPhoneStatePermission[
0]) != PackageManager.PERMISSION_GRANTED) {
-
// 如果没有授予该权限,就去提示用户请求
-
ActivityCompat.requestPermissions(
this, readPhoneStatePermission,
200);
-
}
-
-
//定位权限
-
String[] locationPermission = {Manifest.permission.ACCESS_FINE_LOCATION};
-
if (ContextCompat.checkSelfPermission(
this, locationPermission[
0]) != PackageManager.PERMISSION_GRANTED) {
-
// 如果没有授予该权限,就去提示用户请求
-
ActivityCompat.requestPermissions(
this, locationPermission,
300);
-
}
-
-
String[] ACCESS_COARSE_LOCATION = {Manifest.permission.ACCESS_COARSE_LOCATION};
-
if (ContextCompat.checkSelfPermission(
this, ACCESS_COARSE_LOCATION[
0]) != PackageManager.PERMISSION_GRANTED) {
-
// 如果没有授予该权限,就去提示用户请求
-
ActivityCompat.requestPermissions(
this, ACCESS_COARSE_LOCATION,
400);
-
}
-
-
-
String[] READ_EXTERNAL_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE};
-
if (ContextCompat.checkSelfPermission(
this, READ_EXTERNAL_STORAGE[
0]) != PackageManager.PERMISSION_GRANTED) {
-
// 如果没有授予该权限,就去提示用户请求
-
ActivityCompat.requestPermissions(
this, READ_EXTERNAL_STORAGE,
500);
-
}
-
-
String[] WRITE_EXTERNAL_STORAGE = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
-
if (ContextCompat.checkSelfPermission(
this, WRITE_EXTERNAL_STORAGE[
0]) != PackageManager.PERMISSION_GRANTED) {
-
// 如果没有授予该权限,就去提示用户请求
-
ActivityCompat.requestPermissions(
this, WRITE_EXTERNAL_STORAGE,
600);
-
}
-
-
}
else{
-
//doSdCardResult();
-
}
-
//LocationClient.reStart();
-
}
-
}
效果截图:
左上角红点带头像的就是别人的位置显示
在这篇博客中主要做了两个工作
① 添加显示他人位置的marker
② 将他人头像作为图片合并到marker中进行显示
主要代码:
① 添加他人位置
这段代码可以添加到构造函数中,或者从服务器获取到他人位置后的代码中去
-
//添加他人位置
-
Resources r =
this.getResources();
-
Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.touxiang);
-
addOthersLocation(
double(对方的经度),
double(对方的纬度),bmp(对方的头像bitmap));
主要函数:
-
public void addOthersLocation(double latitute,double longtitute, Bitmap touxiang) {
-
-
Resources r =
this.getResources();
-
Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.icon_geo);
//红点
-
-
// //构建Marker图标
-
// othersCurrentMarker = BitmapDescriptorFactory
-
// .fromResource(R.drawable.icon_geo);
-
-
//构建Marker图标
-
othersCurrentMarker = BitmapDescriptorFactory
-
.fromBitmap(mergeBitmap(bmp,touxiang));
//public static BitmapDescriptor fromBitmap(Bitmap image) + mergeBitmap()
-
-
//定义Maker坐标点
-
LatLng point =
new LatLng(latitute, longtitute);
-
//构建MarkerOption,用于在地图上添加Marker
-
OverlayOptions option =
new MarkerOptions()
-
.position(point)
-
.icon(othersCurrentMarker);
-
//在地图上添加Marker,并显示
-
mBaiduMap.addOverlay(option);
-
}
② 合并标志点图片和用户头像图像
-
//将两张图片合并为一张图片 用作头像
-
private Bitmap mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {
-
int Width = firstBitmap.getWidth();
-
int height = firstBitmap.getHeight();
-
secondBitmap = zoomImage(secondBitmap,Width,height);
-
Bitmap bitmap = Bitmap.createBitmap(Width, height*
2,
-
firstBitmap.getConfig());
-
Canvas canvas =
new Canvas(bitmap);
-
canvas.drawBitmap(secondBitmap,
new Matrix(),
);
-
canvas.drawBitmap(firstBitmap,
0, height,
);
-
return bitmap;
-
}
-
-
//缩放头像图片
-
public Bitmap zoomImage(Bitmap bgimage, double newWidth,
-
double newHeight) {
-
// 获取这个图片的宽和高
-
float width = bgimage.getWidth();
-
float height = bgimage.getHeight();
-
// 创建操作图片用的matrix对象
-
Matrix matrix =
new Matrix();
-
// 计算宽高缩放率
-
float scaleWidth = ((
float) newWidth) / width;
-
float scaleHeight = ((
float) newHeight) / height;
-
// 缩放图片动作
-
matrix.postScale(scaleWidth, scaleHeight);
-
Bitmap bitmap = Bitmap.createBitmap(bgimage,
0,
0, (
int) width,
-
(
int) height, matrix,
true);
-
return bitmap;
-
}
完整代码:
-
import android.Manifest;
-
import android.app.Activity;
-
import android.content.Context;
-
import android.content.pm.PackageManager;
-
import android.content.res.Resources;
-
import android.graphics.Bitmap;
-
import android.graphics.BitmapFactory;
-
import android.graphics.Canvas;
-
import android.graphics.Matrix;
-
import android.graphics.drawable.BitmapDrawable;
-
import android.graphics.drawable.Drawable;
-
import android.hardware.Sensor;
-
import android.hardware.SensorEvent;
-
import android.hardware.SensorEventListener;
-
import android.hardware.SensorManager;
-
import android.os.Build;
-
import android.os.Bundle;
-
import android.support.v4.app.ActivityCompat;
-
import android.support.v4.content.ContextCompat;
-
import android.util.Log;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
import android.widget.CompoundButton;
-
import android.widget.ImageView;
-
import android.widget.RadioGroup.OnCheckedChangeListener;
-
import android.widget.Toast;
-
import android.widget.ToggleButton;
-
import android.widget.ZoomControls;
-
-
import com.baidu.location.BDLocation;
-
import com.baidu.location.BDLocationListener;
-
import com.baidu.location.LocationClient;
-
import com.baidu.location.LocationClientOption;
-
import com.baidu.mapapi.SDKInitializer;
-
import com.baidu.mapapi.map.BaiduMap;
-
import com.baidu.mapapi.map.BitmapDescriptor;
-
import com.baidu.mapapi.map.BitmapDescriptorFactory;
-
import com.baidu.mapapi.map.MapStatusUpdate;
-
import com.baidu.mapapi.map.MapStatusUpdateFactory;
-
import com.baidu.mapapi.map.MapView;
-
import com.baidu.mapapi.map.MarkerOptions;
-
import com.baidu.mapapi.map.MyLocationConfiguration;
-
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
-
import com.baidu.mapapi.map.MyLocationData;
-
import com.baidu.mapapi.map.OverlayOptions;
-
import com.baidu.mapapi.model.LatLng;
-
-
/**
-
* 此demo用来展示如何结合定位SDK实现定位,并使用MyLocationOverlay绘制定位位置 同时展示如何使用自定义图标绘制并点击时弹出泡泡
-
*
-
*/
-
public
class LocationDemo extends Activity {
-
-
// 定位相关
-
LocationClient mLocClient;
-
public MyLocationListenner myListener =
new MyLocationListenner();
-
private LocationMode mCurrentMode;
-
BitmapDescriptor mCurrentMarker,othersCurrentMarker;
-
-
MapView mMapView;
-
BaiduMap mBaiduMap;
-
-
private SensorManager mSensorManager;
-
double degree =
0;
-
// UI相关
-
OnCheckedChangeListener radioButtonListener;
-
Button requestLocButton;
-
ToggleButton togglebtn =
;
-
boolean isFirstLoc =
true;
// 是否首次定位
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
SDKInitializer.initialize(getApplicationContext());
-
setContentView(R.layout.activity_location);
-
judgePermission();
-
requestLocButton = (Button) findViewById(R.id.button1);
-
mCurrentMode = LocationMode.NORMAL;
-
requestLocButton.setText(
"普通");
-
OnClickListener btnClickListener =
new OnClickListener() {
-
public void onClick(View v) {
-
switch (mCurrentMode) {
-
case NORMAL:
-
requestLocButton.setText(
"跟随");
-
mCurrentMode = LocationMode.FOLLOWING;
-
mBaiduMap
-
.setMyLocationConfigeration(
new MyLocationConfiguration(
-
mCurrentMode,
true, mCurrentMarker));
-
break;
-
case COMPASS:
-
requestLocButton.setText(
"普通");
-
mCurrentMode = LocationMode.NORMAL;
-
mBaiduMap
-
.setMyLocationConfigeration(
new MyLocationConfiguration(
-
mCurrentMode,
true, mCurrentMarker));
-
break;
-
case FOLLOWING:
-
requestLocButton.setText(
"罗盘");
-
mCurrentMode = LocationMode.COMPASS;
-
mBaiduMap
-
.setMyLocationConfigeration(
new MyLocationConfiguration(
-
mCurrentMode,
true, mCurrentMarker));
-
break;
-
}
-
}
-
};
-
requestLocButton.setOnClickListener(btnClickListener);
-
-
togglebtn = (ToggleButton) findViewById(R.id.togglebutton);
-
togglebtn
-
.setOnCheckedChangeListener(
new android.widget.CompoundButton.OnCheckedChangeListener() {
-
-
@Override
-
public void onCheckedChanged(CompoundButton buttonView,
-
boolean isChecked) {
-
if (isChecked) {
-
// 普通地图
-
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
-
}
else {
-
// 卫星地图
-
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
-
}
-
-
}
-
});
-
-
// 地图初始化
-
mMapView = (MapView) findViewById(R.id.bmapView);
-
mBaiduMap = mMapView.getMap();
-
// 开启定位图层
-
mBaiduMap.setMyLocationEnabled(
true);
-
// 定位初始化
-
mLocClient =
new LocationClient(
this);
-
mLocClient.registerLocationListener(myListener);
-
LocationClientOption option =
new LocationClientOption();
-
option.setOpenGps(
true);
// 打开gps
-
option.setCoorType(
"bd09ll");
// 设置坐标类型
-
option.setScanSpan(
1000);
// 设置发起定位请求的间隔时间为1000ms
-
option.setIsNeedAddress(
true);
-
mLocClient.setLocOption(option);
-
mLocClient.start();
-
// 隐藏logo
-
View child = mMapView.getChildAt(
1);
-
if (child !=
&& (child
instanceof ImageView || child
instanceof ZoomControls)){
-
child.setVisibility(View.INVISIBLE);
-
}
-
-
//指南针
-
mSensorManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
-
Sensor magenticSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
-
Sensor accelerometerSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
-
mSensorManager.registerListener(listener,magenticSensor,SensorManager.SENSOR_DELAY_GAME);
-
mSensorManager.registerListener(listener,accelerometerSensor,SensorManager.SENSOR_DELAY_GAME);
-
-
//添加他人位置
-
Resources r =
this.getResources();
-
Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.touxiang);
-
addOthersLocation(经度(
double),纬度(
double),头像bmp);
-
}
-
-
//指南针 获取degree值
-
private SensorEventListener listener=
new SensorEventListener() {
-
float[] accelerometerValues=
new
float[
3];
-
float[] magenticValues=
new
float[
3];
-
@Override
-
public void onSensorChanged(SensorEvent sensorEvent) {
-
//判断当前是加速度传感器还是地磁传感器
-
if(sensorEvent.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
-
accelerometerValues=sensorEvent.values.clone();
-
}
else
if(sensorEvent.sensor.getType()== Sensor.TYPE_MAGNETIC_FIELD){
-
magenticValues=sensorEvent.values.clone();
-
}
-
float[] R=
new
float[
9];
-
float[] values=
new
float[
3];
-
SensorManager.getRotationMatrix(R,
, accelerometerValues, magenticValues);
-
SensorManager.getOrientation(R,values);
-
Log.d(
"MainActivity",
"value[0] is"+Math.toDegrees(values[
0]));
-
degree = Math.toDegrees(values[
0]);
-
-
}
-
@Override
-
public void onAccuracyChanged(Sensor sensor, int i) {
-
}
-
};
-
-
public void addOthersLocation(double latitute,double longtitute, Bitmap touxiang) {
-
-
Resources r =
this.getResources();
-
Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.icon_geo);
//标识红点
-
-
// //构建Marker图标
-
// othersCurrentMarker = BitmapDescriptorFactory
-
// .fromResource(R.drawable.icon_geo);
-
-
//构建Marker图标
-
othersCurrentMarker = BitmapDescriptorFactory
-
.fromBitmap(mergeBitmap(bmp,touxiang));
//public static BitmapDescriptor fromBitmap(Bitmap image) + mergeBitmap()
-
-
//定义Maker坐标点
-
LatLng point =
new LatLng(latitute, longtitute);
-
//构建MarkerOption,用于在地图上添加Marker
-
OverlayOptions option =
new MarkerOptions()
-
.position(point)
-
.icon(othersCurrentMarker);
-
//在地图上添加Marker,并显示
-
mBaiduMap.addOverlay(option);
-
}
-
-
//将两张图片合并为一张图片 用作头像
-
private Bitmap mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {
-
int Width = firstBitmap.getWidth();
-
int height = firstBitmap.getHeight();
-
secondBitmap = zoomImage(secondBitmap,Width,height);
-
Bitmap bitmap = Bitmap.createBitmap(Width, height*
2,
-
firstBitmap.getConfig());
-
Canvas canvas =
new Canvas(bitmap);
-
canvas.drawBitmap(secondBitmap,
new Matrix(),
);
-
canvas.drawBitmap(firstBitmap,
0, height,
);
-
return bitmap;
-
}
-
-
//缩放头像图片
-
public Bitmap zoomImage(Bitmap bgimage, double newWidth,
-
double newHeight) {
-
// 获取这个图片的宽和高
-
float width = bgimage.getWidth();
-
float height = bgimage.getHeight();
-
// 创建操作图片用的matrix对象
-
Matrix matrix =
new Matrix();
-
// 计算宽高缩放率
-
float scaleWidth = ((
float) newWidth) / width;
-
float scaleHeight = ((
float) newHeight) / height;
-
// 缩放图片动作
-
matrix.postScale(scaleWidth, scaleHeight);
-
Bitmap bitmap = Bitmap.createBitmap(bgimage,
0,
0, (
int) width,
-
(
int) height, matrix,
true);
-
return bitmap;
-
}
-
-
/**
-
* 定位SDK监听函数
-
*/
-
public
class MyLocationListenner implements BDLocationListener {
-
-
@Override
-
public void onReceiveLocation(BDLocation location) {
-
// map view 销毁后不在处理新接收的位置
-
if (location ==
|| mMapView ==
)
-
return;
-
MyLocationData locData =
new MyLocationData.Builder()
-
.accuracy(location.getRadius())
-
// 此处设置开发者获取到的方向信息,顺时针0-360
-
.direction((
float)degree).latitude(location.getLatitude())
-
.longitude(location.getLongitude()).build();
-
mBaiduMap.setMyLocationData(locData);
-
if (isFirstLoc) {
-
isFirstLoc =
false;
-
//定义Maker坐标点 https://blog.csdn.net/u012382791/article/details/53856203
-
LatLng ll =
new LatLng(location.getLatitude(),
-
location.getLongitude());
-
// MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
-
// 设置缩放比例,更新地图状态
-
float f = mBaiduMap.getMaxZoomLevel();
// 19.0
-
MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll,
-
f -
2);
-
mBaiduMap.animateMapStatus(u);
-
//地图位置显示
-
Toast.makeText(LocationDemo.
this, location.getAddrStr(),
-
Toast.LENGTH_SHORT).show();
-
}
-
-
}
-
-
public void onReceivePoi(BDLocation poiLocation) {
-
}
-
}
-
-
@Override
-
protected void onPause() {
-
mMapView.onPause();
-
super.onPause();
-
}
-
-
@Override
-
protected void onResume() {
-
mMapView.onResume();
-
super.onResume();
-
}
-
-
@Override
-
protected void onDestroy() {
-
// 退出时销毁定位
-
mLocClient.stop();
-
// 关闭定位图层
-
mBaiduMap.setMyLocationEnabled(
false);
-
mMapView.onDestroy();
-
mMapView =
;
-
-
if(mSensorManager!=
){
-
mSensorManager.unregisterListener(listener);
-
}
-
super.onDestroy();
-
}
-
-
-
//6.0之后要动态获取权限,重要!!!
-
protected void judgePermission() {
-
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
-
// 检查该权限是否已经获取
-
// 权限是否已经 授权 GRANTED---授权 DINIED---拒绝
-
-
// sd卡权限
-
String[] SdCardPermission = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
-
if (ContextCompat.checkSelfPermission(
this, SdCardPermission[
0]) != PackageManager.PERMISSION_GRANTED) {
-
// 如果没有授予该权限,就去提示用户请求
-
ActivityCompat.requestPermissions(
this, SdCardPermission,
100);
-
}
-
-
//手机状态权限
-
String[] readPhoneStatePermission = {Manifest.permission.READ_PHONE_STATE};
-
if (ContextCompat.checkSelfPermission(
this, readPhoneStatePermission[
0]) != PackageManager.PERMISSION_GRANTED) {
-
// 如果没有授予该权限,就去提示用户请求
-
ActivityCompat.requestPermissions(
this, readPhoneStatePermission,
200);
-
}
-
-
//定位权限
-
String[] locationPermission = {Manifest.permission.ACCESS_FINE_LOCATION};
-
if (ContextCompat.checkSelfPermission(
this, locationPermission[
0]) != PackageManager.PERMISSION_GRANTED) {
-
// 如果没有授予该权限,就去提示用户请求
-
ActivityCompat.requestPermissions(
this, locationPermission,
300);
-
}
-
-
String[] ACCESS_COARSE_LOCATION = {Manifest.permission.ACCESS_COARSE_LOCATION};
-
if (ContextCompat.checkSelfPermission(
this, ACCESS_COARSE_LOCATION[
0]) != PackageManager.PERMISSION_GRANTED) {
-
// 如果没有授予该权限,就去提示用户请求
-
ActivityCompat.requestPermissions(
this, ACCESS_COARSE_LOCATION,
400);
-
}
-
-
-
String[] READ_EXTERNAL_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE};
-
if (ContextCompat.checkSelfPermission(
this, READ_EXTERNAL_STORAGE[
0]) != PackageManager.PERMISSION_GRANTED) {
-
// 如果没有授予该权限,就去提示用户请求
-
ActivityCompat.requestPermissions(
this, READ_EXTERNAL_STORAGE,
500);
-
}
-
-
String[] WRITE_EXTERNAL_STORAGE = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
-
if (ContextCompat.checkSelfPermission(
this, WRITE_EXTERNAL_STORAGE[
0]) != PackageManager.PERMISSION_GRANTED) {
-
// 如果没有授予该权限,就去提示用户请求
-
ActivityCompat.requestPermissions(
this, WRITE_EXTERNAL_STORAGE,
600);
-
}
-
-
}
else{
-
//doSdCardResult();
-
}
-
//LocationClient.reStart();
-
}
-
}
效果截图:
左上角红点带头像的就是别人的位置显示