80,337
社区成员
发帖
与我相关
我的任务
分享
public class Main extends MapActivity {
// 地图引擎管理
private BMapManager mBMapManager;
// MapView控制
private MapController mMapController;
// MapView
private MapView mMapView;
// 地图覆盖物
private MyLocationOverlay mLocationOverlay;
// 当前经纬度
private GeoPoint myPoint;
// 定位监听
private LocationListener mLocationListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBMapManager = new BMapManager(getApplication());
mBMapManager.init("65E876940AFB41F7DC232FFB11DBCECEC948591B", null);
super.initMapActivity(mBMapManager);
mMapView = (MapView)findViewById(R.id.bmapsView);
// 设置启用内置的缩放控件
mMapView.setBuiltInZoomControls(true);
// 设定缩放时重新绘制覆盖物
mMapView.setDrawOverlayWhenZooming(true);
// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
mMapController = mMapView.getController();
//设置地图zoom级别
mMapController.setZoom(15);
// 添加定位图层
mLocationOverlay = new MyLocationOverlay(this, mMapView);
// 添加定位覆盖物
mMapView.getOverlays().add(mLocationOverlay);
Drawable marker = getResources().getDrawable(R.drawable.shop);
mMapView.getOverlays().add(new ShopOverLay(marker, Main.this));
// 注册定位监听
mLocationListener = new LocationListener() {
public void onLocationChanged(Location mLocation) {
if (mLocation != null) {
double latitude = mLocation.getLatitude();
double longitude = mLocation.getLongitude();
myPoint = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
mMapController.animateTo(myPoint);
// 设置地图中心点
mMapController.setCenter(myPoint);
// 设定比例尺
mMapController.setZoom(16);
}
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
protected void onDestroy() {
if(mBMapManager != null) {
mBMapManager.destroy();
mBMapManager = null;
}
super.onDestroy();
}
@Override
protected void onPause() {
if(mBMapManager != null) {
mBMapManager.getLocationManager().removeUpdates(mLocationListener);
mLocationOverlay.disableMyLocation();
mLocationOverlay.disableCompass();
mBMapManager.stop();
}
super.onPause();
}
@Override
protected void onResume() {
if(mBMapManager != null) {
mBMapManager.getLocationManager().requestLocationUpdates(mLocationListener);
mLocationOverlay.enableMyLocation();
mLocationOverlay.enableCompass();
mBMapManager.start();
}
super.onResume();
}
}