百度地图中的多地图覆盖物的实现

package com.njupt.bmap_helloworld;

import java.util.ArrayList;
import java.util.List;

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

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
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.setTraffic(true);显示交通地图
//		mapView.setSatellite(true);显示卫星地图
		
		Drawable drawable = getResources().getDrawable(R.drawable.iconmarka);
		mapView.getOverlays().add(new MyOverLayItem(drawable));
	}

	public class MyOverLayItem extends ItemizedOverlay<OverlayItem>{

		private List<OverlayItem> list = new ArrayList<OverlayItem>();
		//定义一组坐标,每一个都是double类型
		private double mLat1 = 39.90923;//纬度
		private double mLon1 = 116.397428;//经度
		
		private double mLat2 = 39.9022;
		private double mLon2 = 116.3922;
		
		private double mLat3 = 39.917723;
		private double mLon3 = 116.37222;
		
		//用于在地图上标志坐标,用一个图片标志
		public MyOverLayItem(Drawable drawable) {
			super(drawable);
			//第一组数据在地图上的标注点
			GeoPoint geoPoint1 = new GeoPoint((int)(mLat1*1E6), (int)(mLon1*1E6));
			GeoPoint geoPoint2 = new GeoPoint((int)(mLat2*1E6), (int)(mLon2*1E6));
			GeoPoint geoPoint3 = new GeoPoint((int)(mLat3*1E6), (int)(mLon3*1E6));
			
			list.add(new OverlayItem(geoPoint1, "Point1", "Point1"));
			list.add(new OverlayItem(geoPoint2, "Point2", "Point2"));
			list.add(new OverlayItem(geoPoint3, "Point3", "Point3"));
		    
			populate();//刷新地图,不加的话会报NullPointException
		}
		
		//返回list集合中的每一个坐标
		@Override
		protected OverlayItem createItem(int arg0) {
			return list.get(arg0);
		}

		@Override
		public int size() {
			return list.size();
		}
		
		@Override
		protected boolean onTap(int i) {//设置点击事件
			Toast.makeText(MainActivity.this, list.get(i).getSnippet(), 1).show();
			return true;
		}
	}
	@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;
	}

}

你可能感兴趣的:(百度地图中的多地图覆盖物的实现)