package cn.zengfansheng.mybaidumap.layer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import cn.zengfansheng.mybaidumap.ConstantValue;
import cn.zengfansheng.mybaidumap.R;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint;
/**
* 地图图层
*/
public class LayerDemo extends Activity implements OnClickListener {
private static final String TAG = "LayerDemo";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initManager();
this.setContentView(R.layout.layer);
initViewController();
initComponent();
}
// NOTES====================2、初始化BMapManager,MapController和MapView==================
private BMapManager bMapManager;
private MapController mapController;
private MapView mapView;
/**
* 1、初始化BMapManager
*/
private void initManager() {
bMapManager = new BMapManager(getApplication());
bMapManager.init(ConstantValue.KEY, new MKGeneralListener() {
@Override
public void onGetPermissionState(int iError) {
if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
Toast.makeText(getApplication(), "认证失败",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onGetNetworkState(int iError) {
if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
Toast.makeText(getApplication(), "无网络", Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* 2、初始化MapView,MapController
*/
private void initViewController() {
// 1、获取MapView对象
mapView = (MapView) this.findViewById(R.id.mapview_layer);
// 2、通过MapView获取MapController
mapController = mapView.getController();
// 3、mapView获取放大缩放按钮
mapView.setBuiltInZoomControls(true);
// 4、设置缩放等级
mapController.setZoom(13);
// 5、设置地图的中心
int latitudeE6 = (int) (40.051 * 1E6);// 纬度,乘以10^6
int longitudeE6 = (int) (116.303 * 1E6);// 经度,乘以10^6
GeoPoint geoPoint = new GeoPoint(latitudeE6, longitudeE6);
mapController.setCenter(geoPoint);
}
@Override
protected void onResume() {
super.onResume();
bMapManager.start();
Log.i(TAG, "地图start");
}
@Override
protected void onPause() {
super.onPause();
bMapManager.stop();
Log.i(TAG, "地图stop");
}
@Override
protected void onDestroy() {
super.onDestroy();
bMapManager.destroy();
Log.i(TAG, "地图destroy");
}
// NOTES===========================1、组件初始化===========================
private Button btnBaseLayer;// 底图
private Button btnTrafficLayer;// 交通图
private Button btnSatelliteLayer;// 卫星图
/**
* 初始化组件和设置监听
*/
private void initComponent() {
btnBaseLayer = (Button) this.findViewById(R.id.bt_base_layer);
btnTrafficLayer = (Button) this.findViewById(R.id.bt_traffic_layer);
btnSatelliteLayer = (Button) this.findViewById(R.id.bt_satellite_layer);
btnBaseLayer.setOnClickListener(this);
btnTrafficLayer.setOnClickListener(this);
btnSatelliteLayer.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_base_layer:// 默认展示:底图
mapView.setTraffic(false);
mapView.setSatellite(false);
break;
case R.id.bt_traffic_layer:// 实时交通图
mapView.setTraffic(true);
break;
case R.id.bt_satellite_layer:// 卫星图
mapView.setSatellite(true);
break;
}
}
}