content: package com.amapv2.apis.basic;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemSelectedListener;
  import android.widget.ArrayAdapter;
  import android.widget.CheckBox;
  import android.widget.Spinner;
  
  import com.amap.api.maps.AMap;
  import com.amap.api.maps.MapView;
  import com.amap.api.maps.model.MyTrafficStyle;
  import com.amapv2.apis.R;
  
  /**
   * AMapV2地图中简单介绍矢量地图和卫星地图模式切换
   */
  public class LayersActivity extends Activity implements OnItemSelectedListener,
  		OnClickListener {
  	private AMap aMap;
  	private MapView mapView;
  
  	@Override
  	protected void onCreate(Bundle savedInstanceState) {
  		super.onCreate(savedInstanceState);
  		setContentView(R.layout.layers_activity);
  	    /*
           * 设置离线地图存储目录，在下载离线地图或初始化地图设置;
           * 使用过程中可自行设置, 若自行设置了离线地图存储的路径，
           * 则需要在离线地图下载和使用地图页面都进行路径设置
           * */
  	    //Demo中为了其他界面可以使用下载的离线地图，使用默认位置存储，屏蔽了自定义设置
         // MapsInitializer.sdcardDir =OffLineMapUtils.getSdCacheDir(this);
  		mapView = (MapView) findViewById(R.id.map);
  		mapView.onCreate(savedInstanceState);// 此方法必须重写
  		init();
  	}
  
  	/**
  	 * 初始化AMap对象
  	 */
  	private void init() {
  		if (aMap == null) {
  			aMap = mapView.getMap();
  		}
  		CheckBox traffic = (CheckBox) findViewById(R.id.traffic);
  		traffic.setOnClickListener(this);
  		Spinner spinner = (Spinner) findViewById(R.id.layers_spinner);
  		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
  				this, R.array.layers_array,
  				android.R.layout.simple_spinner_item);
  		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  		spinner.setAdapter(adapter);
  		spinner.setOnItemSelectedListener(this);
  		MyTrafficStyle myTrafficStyle = new MyTrafficStyle();
  		myTrafficStyle.setSeriousCongestedColor(0xff92000a);
  		myTrafficStyle.setCongestedColor(0xffea0312);
  		myTrafficStyle.setSlowColor(0xffff7508);
  		myTrafficStyle.setSmoothColor(0xff00a209);
  		aMap.setMyTrafficStyle(myTrafficStyle);
  	}
  
  	/**
  	 * 方法必须重写
  	 */
  	@Override
  	protected void onResume() {
  		super.onResume();
  		mapView.onResume();
  
  	}
  
  	/**
  	 * 方法必须重写
  	 */
  	@Override
  	protected void onPause() {
  		super.onPause();
  		mapView.onPause();
  	}
  
  	/**
  	 * 方法必须重写
  	 */
  	@Override
  	protected void onSaveInstanceState(Bundle outState) {
  		super.onSaveInstanceState(outState);
  		mapView.onSaveInstanceState(outState);
  	}
  
  	/**
  	 * 方法必须重写
  	 */
  	@Override
  	protected void onDestroy() {
  		super.onDestroy();
  		mapView.onDestroy();
  	}
  
  	@Override
  	public void onItemSelected(AdapterView<?> parent, View view, int position,
  			long id) {
  		if (aMap != null) {
  			setLayer((String) parent.getItemAtPosition(position));
  		}
  	}
  
  	/**
  	 * 选择矢量地图/卫星地图/夜景地图事件的响应
  	 */
  	private void setLayer(String layerName) {
  		if (layerName.equals(getString(R.string.normal))) {
  			aMap.setMapType(AMap.MAP_TYPE_NORMAL);// 矢量地图模式
  		} else if (layerName.equals(getString(R.string.satellite))) {
  			aMap.setMapType(AMap.MAP_TYPE_SATELLITE);// 卫星地图模式
  		}
  		else if(layerName.equals(getString(R.string.night_mode))){
  			aMap.setMapType(AMap.MAP_TYPE_NIGHT);//夜景地图模式
  		}
  	}
  
  	@Override
  	public void onNothingSelected(AdapterView<?> parent) {
  	}
  
  	/**
  	 * 对选择是否显示交通状况事件的响应
  	 */
  	@Override
  	public void onClick(View v) {
  		if (v.getId() == R.id.traffic) {
  			aMap.setTrafficEnabled(((CheckBox) v).isChecked());// 显示实时交通状况
  		}
  	}
  }
  