<application>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="开发者 key" />
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
public class MainActivity extends Activity {
MapView mMapView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
//注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
//获取地图控件引用
mMapView = (MapView) findViewById(R.id.bmapView);
}
@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();
}
}
public class FlyControlBaiduSdk extends Activity{
private MapView mapView;
private BaiduMap mBaiduMap;
private Button normal,site,traffic,myLocation;
private Context context;
//定位相关
private LocationClient locationClient;
private MyLocationListener myLocationListener;
private boolean isFirstIn = true;
private double myLatitude;
private double myLongtitude;
//自定义定位图标相关
private BitmapDescriptor bitmapDescriptor;
private float mCurrentX;
//传感器相关
private MyOrientationlistener mMyOrientationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.flycontrol_baidu);
this.context = this;
initViews();
initLocation();
normal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
}
});
site.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
}
});
traffic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mBaiduMap.isTrafficEnabled()){
mBaiduMap.setTrafficEnabled(false);
}else {
mBaiduMap.setTrafficEnabled(true);
}
}
});
myLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LatLng latLng = new LatLng(myLatitude,myLongtitude);
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
}
});
}
private void initViews(){
mapView = (MapView) findViewById(R.id.bmapView);
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
mBaiduMap = mapView.getMap();
mBaiduMap.setMapStatus(msu);
normal = (Button) findViewById(R.id.normal);
site = (Button) findViewById(R.id.site);
traffic = (Button) findViewById(R.id.traffic);
myLocation = (Button) findViewById(R.id.myLocation);
}
private void initLocation(){
locationClient = new LocationClient(this);
myLocationListener = new MyLocationListener();
locationClient.registerLocationListener(myLocationListener);
LocationClientOption locationClientOption = new LocationClientOption();
locationClientOption.setCoorType("bd09ll");
locationClientOption.setIsNeedAddress(true);
locationClientOption.setOpenGps(true);
locationClientOption.setScanSpan(1000);
locationClient.setLocOption(locationClientOption);
//初始化图标
bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.navii);
mMyOrientationListener = new MyOrientationlistener(this);
mMyOrientationListener.setOnOrientationListener(new MyOrientationlistener.OnOrientationListener() {
@Override
public void onOrientationChange(float x) {
mCurrentX = x;
}
});
}
//初始化定位
@Override
protected void onStart() {
super.onStart();
//开始定位
mBaiduMap.setMyLocationEnabled(true);
if (!locationClient.isStarted()){
locationClient.start();}
//开始方向传感器
mMyOrientationListener.start();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
//停止定位
@Override
protected void onStop() {
super.onStop();
//停止定位
mBaiduMap.setMyLocationEnabled(false);
locationClient.stop();
//停止方向传感器
mMyOrientationListener.stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
//定位监听类
private class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation bdLocation) {
MyLocationData myLocationData = new MyLocationData.Builder().accuracy(bdLocation.getRadius())//
.direction(mCurrentX).latitude(bdLocation.getLatitude()).longitude(bdLocation.getLongitude()).build();
mBaiduMap.setMyLocationData(myLocationData);
//设置经纬度
myLatitude = bdLocation.getLatitude();
myLongtitude = bdLocation.getLongitude();
//设置定位图标
MyLocationConfiguration configuration = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL,true,bitmapDescriptor);
mBaiduMap.setMyLocationConfigeration(configuration);
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();
}
}
}
}
public class MyOrientationlistener implements SensorEventListener{
private SensorManager mSensorManager;
private Sensor mSensor;
private Context mContext;
private float lastX;
public MyOrientationlistener(Context context){
this.mContext = context;
}
public void start(){
mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager != null){
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
if (mSensor != null){
mSensorManager.registerListener(this,mSensor,SensorManager.SENSOR_DELAY_UI);
}
}
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 (onOrientationListener != null){
onOrientationListener.onOrientationChange(x);
}
}
lastX = x;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private OnOrientationListener onOrientationListener;
public void setOnOrientationListener(OnOrientationListener onOrientationListener) {
this.onOrientationListener = onOrientationListener;
}
public interface OnOrientationListener{
void onOrientationChange(float x);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:clickable="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:text="我的位置"
android:id="@+id/myLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/button_back"/>
<Button
android:text="普通地图"
android:id="@+id/normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/button_back"/>
<Button
android:text="卫星地图"
android:id="@+id/site"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/button_back"/>
<Button
android:text="实时交通"
android:id="@+id/traffic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/button_back"/>
</LinearLayout>
</LinearLayout>
版权声明:本文为博主原创文章,未经博主允许不得转载。
使用百度地图开发一个导航定位demo-android学习之旅(77)
原文地址:http://blog.csdn.net/lpjishu/article/details/48008337