content: package com.amapv2.apis.geocoder;
  
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  
  import com.amap.api.maps.AMap;
  import com.amap.api.maps.CameraUpdateFactory;
  import com.amap.api.maps.MapView;
  import com.amap.api.maps.model.BitmapDescriptorFactory;
  import com.amap.api.maps.model.Marker;
  import com.amap.api.maps.model.MarkerOptions;
  import com.amap.api.services.core.LatLonPoint;
  import com.amap.api.services.geocoder.GeocodeAddress;
  import com.amap.api.services.geocoder.GeocodeQuery;
  import com.amap.api.services.geocoder.GeocodeResult;
  import com.amap.api.services.geocoder.GeocodeSearch;
  import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
  import com.amap.api.services.geocoder.RegeocodeQuery;
  import com.amap.api.services.geocoder.RegeocodeResult;
  import com.amapv2.apis.R;
  import com.amapv2.apis.util.AMapUtil;
  import com.amapv2.apis.util.ToastUtil;
  
  /**
   * 地理编码与逆地理编码功能介绍
   */
  public class GeocoderActivity extends Activity implements
  		OnGeocodeSearchListener, OnClickListener {
  	private ProgressDialog progDialog = null;
  	private GeocodeSearch geocoderSearch;
  	private String addressName;
  	private AMap aMap;
  	private MapView mapView;
  	private LatLonPoint latLonPoint = new LatLonPoint(39.90865, 116.39751);
  	private Marker geoMarker;
  	private Marker regeoMarker;
  
  	@Override
  	protected void onCreate(Bundle savedInstanceState) {
  		super.onCreate(savedInstanceState);
  		setContentView(R.layout.geocoder_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();
  			geoMarker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
  					.icon(BitmapDescriptorFactory
  							.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
  			regeoMarker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
  					.icon(BitmapDescriptorFactory
  							.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
  		}
  		Button geoButton = (Button) findViewById(R.id.geoButton);
  		geoButton.setOnClickListener(this);
  		Button regeoButton = (Button) findViewById(R.id.regeoButton);
  		regeoButton.setOnClickListener(this);
  		geocoderSearch = new GeocodeSearch(this);
  		geocoderSearch.setOnGeocodeSearchListener(this);
  		progDialog = new ProgressDialog(this);
  	}
  
  	/**
  	 * 方法必须重写
  	 */
  	@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();
  	}
  
  	/**
  	 * 显示进度条对话框
  	 */
  	public void showDialog() {
  		progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  		progDialog.setIndeterminate(false);
  		progDialog.setCancelable(true);
  		progDialog.setMessage("正在获取地址");
  		progDialog.show();
  	}
  
  	/**
  	 * 隐藏进度条对话框
  	 */
  	public void dismissDialog() {
  		if (progDialog != null) {
  			progDialog.dismiss();
  		}
  	}
  
  	/**
  	 * 响应地理编码
  	 */
  	public void getLatlon(final String name) {
  		showDialog();
  
  		GeocodeQuery query = new GeocodeQuery(name, "010");// 第一个参数表示地址，第二个参数表示查询城市，中文或者中文全拼，citycode、adcode，
  		geocoderSearch.getFromLocationNameAsyn(query);// 设置同步地理编码请求
  	}
  
  	/**
  	 * 响应逆地理编码
  	 */
  	public void getAddress(final LatLonPoint latLonPoint) {
  		showDialog();
  		RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200,
  				GeocodeSearch.AMAP);// 第一个参数表示一个Latlng，第二参数表示范围多少米，第三个参数表示是火系坐标系还是GPS原生坐标系
  		geocoderSearch.getFromLocationAsyn(query);// 设置同步逆地理编码请求
  	}
  
  	/**
  	 * 地理编码查询回调
  	 */
  	@Override
  	public void onGeocodeSearched(GeocodeResult result, int rCode) {
  		dismissDialog();
  		if (rCode == 0) {
  			if (result != null && result.getGeocodeAddressList() != null
  					&& result.getGeocodeAddressList().size() > 0) {
  				GeocodeAddress address = result.getGeocodeAddressList().get(0);
  				aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
  						AMapUtil.convertToLatLng(address.getLatLonPoint()), 15));
  				geoMarker.setPosition(AMapUtil.convertToLatLng(address
  						.getLatLonPoint()));
  				addressName = "经纬度值:" + address.getLatLonPoint() + "\n位置描述:"
  						+ address.getFormatAddress();
  				ToastUtil.show(GeocoderActivity.this, addressName);
  			} else {
  				ToastUtil.show(GeocoderActivity.this, R.string.no_result);
  			}
  		} else if (rCode == 27) {
  			ToastUtil.show(GeocoderActivity.this, R.string.error_network);
  		} else if (rCode == 32) {
  			ToastUtil.show(GeocoderActivity.this, R.string.error_key);
  		} else {
  			ToastUtil.show(GeocoderActivity.this,
  					getString(R.string.error_other) + rCode);
  		}
  	}
  
  	/**
  	 * 逆地理编码回调
  	 */
  	@Override
  	public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
  		dismissDialog();
  		if (rCode == 0) {
  			if (result != null && result.getRegeocodeAddress() != null
  					&& result.getRegeocodeAddress().getFormatAddress() != null) {
  				addressName = result.getRegeocodeAddress().getFormatAddress()
  						+ "附近";
  				aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
  						AMapUtil.convertToLatLng(latLonPoint), 15));
  				regeoMarker.setPosition(AMapUtil.convertToLatLng(latLonPoint));
  				ToastUtil.show(GeocoderActivity.this, addressName);
  			} else {
  				ToastUtil.show(GeocoderActivity.this, R.string.no_result);
  			}
  		} else if (rCode == 27) {
  			ToastUtil.show(GeocoderActivity.this, R.string.error_network);
  		} else if (rCode == 32) {
  			ToastUtil.show(GeocoderActivity.this, R.string.error_key);
  		} else {
  			ToastUtil.show(GeocoderActivity.this,
  					getString(R.string.error_other) + rCode);
  		}
  	}
  
  	@Override
  	public void onClick(View v) {
  		switch (v.getId()) {
  		/**
  		 * 响应地理编码按钮
  		 */
  		case R.id.geoButton:
  			getLatlon("方恒国际中心");
  			break;
  		/**
  		 * 响应逆地理编码按钮
  		 */
  		case R.id.regeoButton:
  			getAddress(latLonPoint);
  			break;
  		default:
  			break;
  		}
  	}
  }