package com.Cal.bdmap;
import android.Manifest;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.NetworkUtil;
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.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.platform.comapi.map.B;
public class MainActivity extends AppCompatActivity {
private MapView mMapView = null;
private BaiduMap mBaiduMap;
private Button normal;
private Button satelite;
private Button traffic;
private Context context;
private LocationClient mLocationClient;
private MyLocationLisener mLocationLisener;
private boolean isFirstIn = true;
private double mLatitude;
private double mlongtitude;
private String address;
private BitmapDescriptor mIconLocationg;
private MyOrientationListener mMyOrientationListener;
private float mCurrendX;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.main);
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
normal = (Button) findViewById(R.id.map_normal);
satelite = (Button) findViewById(R.id.map_satelite);
traffic = (Button) findViewById(R.id.map_traffic);
//初始化图标
mIconLocationg = BitmapDescriptorFactory.fromResource(R.mipmap.huaji);
mMyOrientationListener = new MyOrientationListener(context);
mMyOrientationListener.setmOnOrientationListener(new MyOrientationListener.OnOrientationListener() {
@Override
public void onOrientationChang(float x) {
mCurrendX = x;
}
});
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.zoomTo(15.0f);
mBaiduMap.setMapStatus(mapStatusUpdate);
this.context = this;
initLocationd();
//动态获取权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 123);
} else {
Toast.makeText(this, "已经获取权限", Toast.LENGTH_SHORT).show();
}
}
private void initLocationd() {
mLocationClient = new LocationClient(this);
mLocationLisener = new MyLocationLisener();
mLocationClient.registerLocationListener(mLocationLisener);
LocationClientOption option = new LocationClientOption();
option.setCoorType("bd09ll");
option.setIsNeedAddress(true);
option.setOpenGps(true);
option.setScanSpan(1000);
mLocationClient.setLocOption(option);
}
public void doClick(View v) {
switch (v.getId()) {
case R.id.map_normal:
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
break;
case R.id.map_satelite:
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
break;
case R.id.map_traffic:
if (mBaiduMap.isBaiduHeatMapEnabled()) {
mBaiduMap.setTrafficEnabled(false);
Toast.makeText(this, "关闭实时路况", Toast.LENGTH_SHORT).show();
} else if (!mBaiduMap.isBaiduHeatMapEnabled()) {
mBaiduMap.setTrafficEnabled(true);
Toast.makeText(this, "开启实时路况", Toast.LENGTH_SHORT).show();
}
break;
case R.id.mao_location:
ceneterToMyLocation(mLatitude, mlongtitude);
Toast.makeText(this, address, Toast.LENGTH_SHORT).show();
break;
}
}
private void ceneterToMyLocation(double mLatitude, double mlongtitude) {
LatLng latLng = new LatLng(mLatitude, mlongtitude);
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
}
// @Override
// public boolean onOptionsItemSelected(MenuItem item) {
// switch (item.getItemId()) {
// case R.id.common:
// mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
// break;
// case R.id.site:
// mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
// break;
// case R.id.traffic:
// if (mBaiduMap.isBaiduHeatMapEnabled()) {
// mBaiduMap.setTrafficEnabled(false);
// item.setTitle("实时交通(off)");
// } else {
// mBaiduMap.setTrafficEnabled(true);
// item.setTitle("实时交通(on)");
// }
// break;
//
// }
// return super.onOptionsItemSelected(item);
// }
@Override
protected void onStart() {
super.onStart();
mBaiduMap.setMyLocationEnabled(true);
if (!mLocationClient.isStarted()) {
mLocationClient.start();
}
mMyOrientationListener.start();
}
@Override
protected void onStop() {
super.onStop();
mBaiduMap.setMyLocationEnabled(false);
mLocationClient.stop();
mMyOrientationListener.stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
private class MyLocationLisener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
MyLocationData data = new MyLocationData.Builder()
.direction(mCurrendX)
.accuracy(bdLocation.getRadius())
.latitude(bdLocation.getLatitude())
.longitude(bdLocation.getLongitude()).build();
bdLocation.getLocType();
mBaiduMap.setMyLocationData(data);
//设置自定义图标
MyLocationConfiguration config = new MyLocationConfiguration
(MyLocationConfiguration.LocationMode.NORMAL, true, mIconLocationg);
mBaiduMap.setMyLocationConfiguration(config);
mLatitude = bdLocation.getLatitude();
mlongtitude = bdLocation.getLongitude();
if (isFirstIn) {
LatLng latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
isFirstIn = false;
Toast.makeText(context, bdLocation.getAddrStr(), Toast.LENGTH_LONG).show();
address = bdLocation.getAddrStr();
}
}
@Override
public void onConnectHotSpotMessage(String s, int i) {
}
}
}
package com.Cal.bdmap;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
/**
* Created by Cal-Lightman on 2017/4/15.
*/
public class MyOrientationListener implements SensorEventListener {
private SensorManager mSensorManager;
private Context context;
private Sensor sensor;
private float lastX;
private float lastY;
public MyOrientationListener(Context context) {
this.context = context;
}
public void start() {
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager != null) {
sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
if (sensor != null) {
mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);
}
}
public void stop() {
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
float x = event.values[SensorManager.DATA_X];
if (Math.abs(x - lastX) > 1.0) {
if (mOnOrientationListener != null) {
mOnOrientationListener.onOrientationChang(x);
}
}
lastX = x;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void setmOnOrientationListener(OnOrientationListener mOnOrientationListener) {
this.mOnOrientationListener = mOnOrientationListener;
}
private OnOrientationListener mOnOrientationListener;
public interface OnOrientationListener {
void onOrientationChang(float x);
}
}
一条小咸鱼
相关分类