百度地图覆盖物的描述

package com.njupt.bmap_helloworld;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.Overlay;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends MapActivity {

	private MapView mapView;//百度地图的相关控件
	private BMapManager bMapManager;//加载地图的引擎
	private String keyString = "04763B1D8C925195965E838C6E59DB767DAE4BA7";//百度地图的key
	private MapController mapController;//在百度地图上添加一些控件,比如放大或缩小控件
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		mapView = (MapView) findViewById(R.id.bmapView);
		bMapManager = new BMapManager(MainActivity.this);
		
		//必须要加载key
		bMapManager.init(keyString, new MKGeneralListener() {
			
			@Override
			public void onGetPermissionState(int arg0) {
				if(arg0 == 300){
					Toast.makeText(MainActivity.this, "输入的key有误,请核实...", 1).show();
				}
			}
			
			@Override
			public void onGetNetworkState(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		initMapActivity(bMapManager);
		mapView.setBuiltInZoomControls(true);//表示可以设置缩放功能
		mapController = mapView.getController();
		mapController.setZoom(12);
		mapView.getOverlays().add(new MyOverLay());
		
//		mapView.setTraffic(true);显示交通地图
//		mapView.setSatellite(true);显示卫星地图
	}

	public class MyOverLay extends Overlay{
		//构建地图上的一个点
		private GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6), (int)(116.404*1E6));
		//声明一个画笔工具
		private Paint paint = new Paint();
		
		@Override
		public void draw(Canvas arg0, MapView arg1, boolean arg2) {
			super.draw(arg0, arg1, arg2);
			
			Point point = mapView.getProjection().toPixels(geoPoint, null);
			arg0.drawText("*这里是天安门", point.x, point.y, paint);
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		if(bMapManager != null){
			bMapManager.destroy();
			bMapManager = null;
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		if(bMapManager != null){
			bMapManager.start();
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		
		if(bMapManager != null){
			bMapManager.stop();
		}
	}
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

你可能感兴趣的:(百度地图覆盖物的描述)